Can someone explain this piece of code?

In the third person demo of ue4 code i finded this piece of code:
// Create a camera boom (pulls in towards the player if there is a collision)
CameraBoom = PCIP.CreateDefaultSubobject<USpringArmComponent>(this, TEXT(“CameraBoom”));
CameraBoom->AttachTo(RootComponent);
CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character
CameraBoom->bUseControllerViewRotation = true; // Rotate the arm based on the controller

// Create a follow camera
FollowCamera = PCIP.CreateDefaultSubobject&lt;UCameraComponent&gt;(this, TEXT("FollowCamera"));
FollowCamera-&gt;AttachTo(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation
FollowCamera-&gt;bUseControllerViewRotation = false; // Camera does not rotate relative to arm

More precisely, I do not understand CameraBoom->TargetArmLength = 300.0f
what is TargetArmLength?

It says in the code comment - distance between the camera and the player it follows, which is defined in CameraBoom’s TargetArmLength is set to 300 uus.

Just in case, this is how the rea life camera boom looks like:

I know what this variable do,what i don’t know is how it regulate the distance from the player end how all the code work.

_Lynx gave you a quite extensive answer. The distance from the player is regulated by the value you give to “TargetArmLength” (which is in UU: UnrealUnits).

What else do you want to know?

If you mean that you want to know how the code adjusts the distance, take a look at the first line of code:


CameraBoom = PCIP.CreateDefaultSubobject<**USpringArmComponent**>(this, TEXT("CameraBoom"));

What it does:
Takes variable CameraBoom and creates for it an object of USpringArmComponent class. The camera position adjustment is handled by the code in this object. So if you want to see what it does, find the source for USpringArmComponent and look there.

To change the distance change the 300 to another value… 50, 500, 800,what ever works for you.

Creates the object


CameraBoom = PCIP.CreateDefaultSubobject<USpringArmComponent>(this, TEXT("CameraBoom"));

Attaches it to the root component so will follow its movement


CameraBoom->AttachTo(RootComponent);

Look the picture, length of the stick


CameraBoom->TargetArmLength = 300.0f; // The camera follows at this distance behind the character	

the comment explains


CameraBoom->bUseControllerViewRotation = true; // Rotate the arm based on the controller

Create a follow camera, FollowCamera and CameraBoom are declared at the header file.


FollowCamera = PCIP.CreateDefaultSubobject<UCameraComponent>(this, TEXT("FollowCamera"));

Attach the camera in a specific socket


FollowCamera->AttachTo(CameraBoom, USpringArmComponent::SocketName); // Attach the camera to the end of the boom and let the boom adjust to match the controller orientation


FollowCamera->bUseControllerViewRotation = false; // Camera does not rotate relative to arm



myship* ship = Cast<myship>(this->GetPawn());
if (ship != NULL)
{
float currentlength = ship->CameraBoom->TargetArmLength;
float newLength = currentlength - (ZoomSpeed * UGameplayStatics::GetWorldDeltaSeconds(this));
if (newLength < 100.0f) newLength = 100.0f;
float appliedBoomLength = FMath::FInterpTo(newLength, newLength, UGameplayStatics::GetWorldDeltaSeconds(this), 5);
ship->CameraBoom->TargetArmLength = appliedBoomLength;
}


this is how I move my camera in c++