- From the Modes tab, under the Place section and under Basic, drag and drop a Cube object into your scene.
- From the Details tab, go to the Transform component and change the Mobility property to Movable.
- Afterwards, click on the Add Component button and select New C++ Component.
- From the menu that pops up, select Actor Component and select Next:
- From there, give your component a name, for example, RotateActorComponent, and then press the Create Class button.
- Construct your FRotator. FRotators can be constructed using a stock pitch, yaw, and roll constructor, as shown in the following example:
FRotator( float InPitch, float InYaw, float InRoll );
- Your FRotator will be constructed as follows:
FRotator rotator( 0, GetWorld()->TimeSeconds, 0 );
- The standard orientation for an object in UE4 is with Forward facing down the +X-axis. Right is the +Y-axis, and Up is +Z.
- Pitch is rotation about the Y-axis (across), yaw is rotation about the Z-axis (up), and roll is rotation about the X-axis. This is best understood in the following three points:
- Pitch: If you think of an airplane in UE4 standard coordinates, the Y-axis goes along the wingspan (pitching tilts it forward and backward)
- Yaw: The Z-axis goes straight up and down (yawing turns it left and right)
- Roll: The X-axis goes straight along the fuselage of the plane (rolling does barrel rolls)
You should note that in other conventions, the X-axis is pitch, the Y-axis is yaw, and the Z-axis is roll.
- Apply your FRotator to your actor using the SetActorRotation member function, as follows:
// Called every frame
void URotateActorComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
FRotator rotator(0, GetWorld()->TimeSeconds, 0);
GetOwner()->SetActorRotation(rotator);
}