Download

How to control a camera directly from the player controller?

I want to override the CalcCamera() method in my custom player controller class, so that I can set the camera’s location and rotation directly from my code.


void ATycoonPlayerController::BeginPlay()
{
    SetViewTarget(this);
}

void ATycoonPlayerController::CalcCamera(float DeltaTime, FMinimalViewInfo& OutResult)
{
    const FVector camLoc(-270.0f, 270.0f, 400.0f);
    const FRotator camRot(0.0f, -70.0f, 0.0f);

    OutResult.Location = camLoc;
    OutResult.Rotation = camRot;
    OutResult.FOV = 90.0f;
}

However, that does not change the camera’s location nor rotation.
Am I missing something?

For the records, when I create a custom Blueprint class deriving from PlayerCameraManager and do the following in the “Blueprint Update Camera” function, then it works:

Capture.PNG

But how can I achieve exactly that in C++ with the player controller?

Many ways to do it. I’ve done a controllable “drone camera” the following way:

  • PrimaryPlayerController handles all input and feeds it to a currently possessed Character
  • the “drone camera” is just a Character with movement mode set to Flying and small mesh/collision capsule

Funneling PlayerController commands to a possessed character it makes it easy to possess and control many different characters.

The Player Camera Manager is ultimately what determines the final view, it’s likely that whatever you’re doing in the Player Controller is being overridden in there. Have a look into the UpdateCamera function.

PCM is quite extensible, though usually you do have to copy-paste some blocks of code from the main class.