UE4C + +: camera

10/14/2020

video camera

Camera represents the player's perspective; How players see the world. Therefore, the camera is only related to human controlled players. The PlayerController specifies the camera class and instantiates the Camera Actor (used to calculate the position and direction from which the player looks at the world).

PlayerController created Camera

Default player controller

The player controller will be generated by default in the level, and the player controller will automatically generate PlayerCameraManager, that is, the camera.
Actors under the world program:

Details under PlayerController:

PlayerCameraManager

The playercamerager class is a camera management class

ViewTarget

The ViewTarget structure is defined in playercamerager and is responsible for providing an ideal perspective (POV) to playercamerager.

Camera assembly

All properties and behaviors of the camera are created in CameraComponent; The essence of CameraActor is that the Actor attaches a CameraComponent component, so the camera component is the fundamental.

  • Transform attribute
  • Perspective or orthographic mode
  • Post processing effect
  • Visual display of camera

code

UPROPERTY(EditAnywhere)
AActor* CameraOne;
UPROPERTY(EditAnywhere)
AActor* CameraTwo;

//Find actor s that handle local player control
APlayerController* OurPlayerController = UGameplayStatics::GetPlayerController(this, 0);

if (OurPlayerController) 
{
	if (OurPlayerController->GetViewTarget() != CameraOne && CameraOne != nullptr) 
	{
		OurPlayerController->SetViewTarget(CameraOne); //AActor* CameraOne
		
	}
	else if (OurPlayerController->GetViewTarget() != CameraTwo && CameraTwo != nullptr) 
	{
		OurPlayerController->SetViewTargetWithBlend(CameraTwo, SmoothBlendTime);
	}
}
  • Ugameplaystatistics saves the static library related to UE4 and can directly obtain the current PlayerController
  • The SetViewTarget function can use the camera component on the Actor, that is, switch to the view on the Actor

Camera responsibility chain

If the ViewTarget is a CameraActor or an Actor containing CameraComponent and bfindcameracomponentwhenview target is set to_ true_, CameraComponent will provide information about camera properties. "Btakecameracontrolwhenpossed" is a related property that can be set for any Pawn. When occupied by PlayerController, Pawn will automatically become ViewTarget.

ViewTarget indicates the Actor that the camera mainly follows

PawnWithCamera example

The example comes from the camera controlled by UE4 players. See the end of the article

  • USpringArmComponent
  • UCameraComponent
FVector2D CameraInput; //Save input quantity
//Each player moves the mouse, and the amount of movement is saved by CameraInput
void APawnWithCamera::PitchCamera(float AxisValue)
{
    CameraInput.Y = AxisValue;
}

void APawnWithCamera::YawCamera(float AxisValue)
{
    CameraInput.X = AxisValue;
}

//Update Camera under Tick function
//Rotate the Actor's Yaw, which is attached to it, so it will rotate the camera
{
    FRotator NewRotation = GetActorRotation();
    NewRotation.Yaw += CameraInput.X;
    SetActorRotation(NewRotation);
}
 //Rotate the camera's Pitch, but limit it, so the visual fixation is downward
 {
     FRotator NewRotation = OurCameraSpringArm->GetComponentRotation();
     NewRotation.Pitch = FMath::Clamp(NewRotation.Pitch + CameraInput.Y, -80.0f, -15.0f);
     OurCameraSpringArm->SetWorldRotation(NewRotation);
 }
  • Change the rotation of the Actor. Since the Actor has a CameraComponent, it is transferred to additional components

For each movement and rotation, it is necessary to clearly know which component of the Actor is targeted

reference material

UE4 SetViewTarget function
UE4 camera
UE4 player controlled camera

Tags: C++ UE4

Posted by alconebay on Wed, 11 May 2022 15:49:04 +0300