Explanation of The Following Code

I rewatched the video a few times but still don’t get the process behind the code under the BeginPlay():

InitialYaw = GetOwner()->GetActorRotation().Yaw;

CurrentYaw = InitialYaw;

TargetYaw = InitialYaw + 90.f;

and the code in our TickComponent() function:

CurrentYaw = FMath::Lerp(CurrentYaw, TargetYaw, 0.02f);

FRotator DoorRotation = GetOwner()->GetActorRotation();

DoorRotation.Yaw = CurrentYaw;

GetOwner()->SetActorRotation(DoorRotation);

I get that we are initializing under the BeginPlay function but what exactly is happening on the TickComponent side of things?

Hello ! in the Tick part of the code :

// CurrentYaw is a value that is used for where the door is while its travelling between two points - the current Yaw rotation value and the target yaw rotation value. Using the Lerp() function (short for linear interpolation) that is contained in the FMath library , we are able to set CurrentYaw to a different value each tick - which makes the door open over time.

CurrentYaw = FMath::Lerp(CurrentYaw, TargetYaw, 0.02f);

// Here we create a Struct of type FRotator called DoorRotation and initialize it to the current actor’s rotation. The FRotator struct has a pitch, yaw, and a roll value, much like an FVector, but the FRotator is regarding rotation, not location.

FRotator DoorRotation = GetOwner()->GetActorRotation();

// We are going to use the “.” operator to access the yaw value of the DoorRotation struct we just created and set it to the value of CurrentYaw - this is what makes the door rotate along it’s yaw axis over time.

DoorRotation.Yaw = CurrentYaw;

// Finally, the function that ties it all together. We use SetActorRotation every tick with DoorRotation as the rotation value, which is changing with DeltaTime.

GetOwner()->SetActorRotation(DoorRotation);

So, anything that makes something move or needs to track movement usually has to take place in the tick , because movements happen frame-by-frame.

1 Like

Thank you very much!! Makes a lot more sense now!! Appreciate it!!

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms