How to leave the camera fixed and not moving with the Pawn?

Hi guys, i am doing a first project, a simple Arkanoid:
My player(a brick) -> Pawn(sceneComponent,staticMeshComponent,CameraComponent: sceneComponent is Root, the rest are attached to Root)

The background where the “enemy” bricks will spawn (AActor : sceneComponent,StaticMeshComponent)

i spawn those classes completly in C++, NOTHING of blueprints(that is the idea). My player brick moves perfectly but the camera follow the “horizontal” movement of my Pawn. i Tried instead of moving with this->getActorLocation.Y , trying to move accesing directly the staticMesh
using this->myStaticMeshComponent->getlocation.Y , but this doesnt seems to work, nothing happens. Here some Code:
I Already searched the forum and used Google but other user´s cases doesnt seems to help in my case

“Tablero” is the backGround where the enemy bricks will spawn:
ATablero::ATablero()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
PrimaryActorTick.bCanEverTick = true;
OurVisibleComponent = CreateDefaultSubobject<USceneComponent>(TEXT(“Scenecomponent”));
tablero = CreateDefaultSubobject<UStaticMeshComponent>(TEXT(“Tablero”));

RootComponent=OurVisibleComponent;
tablero-&gt;AttachTo(RootComponent);

}
-----------------------------*------------------
AMyPawn::AMyPawn()
{
// Set this pawn to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
PrimaryActorTick.bCanEverTick = true;
// Set this pawn to be controlled by the lowest-numbered player
AutoPossessPlayer = EAutoReceiveInput::Player0;

// Create a dummy root component we can attach things to.
RootComponent = CreateDefaultSubobject&lt;USceneComponent&gt;(TEXT("RootComponent"));
// Create a camera and a visible object
OurCamera = CreateDefaultSubobject&lt;UCameraComponent&gt;(TEXT("OurCamera"));
player = CreateDefaultSubobject&lt;UStaticMeshComponent&gt;(TEXT("OurVisibleComponent"));
// Attach our camera and visible object to our root component. Offset and rotate the camera.
//OurCamera-&gt;AttachTo(RootComponent);
OurCamera-&gt;SetRelativeLocation(FVector(-250.0f, 0.0f,0.0f));
OurCamera-&gt;SetRelativeRotation(FRotator(0.0f, 0.0f, 0.0f));
player-&gt;AttachTo(RootComponent);

}

// Called when the game starts or when spawned
void AMyPawn::BeginPlay()
{
Super::BeginPlay();/*
APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0);
if (OurPlayerController != NULL){
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, TEXT(“This is an on screen message!”));

	//OurPlayerController-&gt;SetViewTarget(OurCamera);
}
*/

}

// Called every frame
void AMyPawn::Tick( float DeltaTime )
{
Super::Tick( DeltaTime );

}

// Called to bind functionality to input
void AMyPawn::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
InputComponent->BindAxis(“movimientoHorizontal”, this, &AMyPawn::movimiento); //movimientohorizontal means HorizontalMovement

}

void AMyPawn::movimiento(float value){ //movimiento means movement

if (value &gt; 0){

//Like i said i tried using simply this->GetActorLocation() and following the same path but didnt worked.
FVector locationAux=player->RelativeLocation;
locationAux.Y = locationAux.Y + 250 * GetWorld()->DeltaTimeSeconds;
//FVector aux = this->GetActorLocation();
//aux.Y = aux.Y + 250 * GetWorld()->DeltaTimeSeconds;
//this->SetActorLocation(aux);
GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT(“%f”), locationAux.Y));
player->SetMobility(EComponentMobility::Movable);
player->RelativeLocation.Y = locationAux.Y;
//locationAux = player->RelativeLocation = locationAux;
}

}
-------------------------------**------------------------

I’m sure there are nicer ways to do this that someone will post eventually, but what I’ve done in my project is to create a custom CameraActor which I place in the world, and then set that as the viewtarget for the playercontroller.

In my player controller Tick function I do this:



void ACustomPlayerController::Tick(float DeltaTime)
{
	Super::Tick(DeltaTime);
	if (settings != NULL && settings->MainCamera != NULL)		//?? do we really need to do this every tick?
	{
		SetViewTarget(settings->MainCamera);
	}
}


I do it every Tick because when I tried setting it in BeginPlay just once it sometimes got “undone” and messed up. Not sure why, but this worked and hasn’t caused any issues. You can then just move the camera and adjust it’s FOV etc in the level and can be confident it won’t move.

I recommend you do what AusPaco says, spawn a CameraActor and set your player controller’s view target to that, because you need a camera that is separated from the pawn.

To add to that and also solve your issue AusPaco, you have to disable the player controller from automatically managing the view target. By default the player controller automatically sets the view target in this way:

  • To a pawn at the moment of possession, even if you’re currently using a custom view target
  • To itself at the moment of unpossession, again even if you’re currently using a custom view target

To prevent this, simply set the PlayerController’s variable bAutoManageActiveCameraTarget to false. Here is what I do:



	// Use separete camera actor as view target
	PlayerController->SetViewTargetWithBlend(MyCameraActor, 0.6f, EViewTargetBlendFunction::VTBlend_EaseIn, 0.5f, true);

	// Prevent player controller from automatically managing the view target
	PlayerController->bAutoManageActiveCameraTarget = false;


Nice tip NisshokuZK, thanks!

Thank you very much guys! i will try this right now. From what i understood, to get a fixed camera i will need 4 classes:
AActor for the board
AActor/UcameraComponent for the camera itself,
APawn to script the movement
APlayerController to manage the Pawn and set the camera as i want.

I will code this and check what happen. Thanks boys!

No problem. :slight_smile:

You’re welcome. You misunderstood one part and thats that there exists already an acter class ACameraActor, which is simply an actor with a camera component. Its basic to do it yourself, but you can save yourself the trouble by using ACameraActor.

Ey guys, if you have time, check this post:

i am running in some troubles i cant fix myself :frowning: related to this post