SUCIALISM

Logo

Hello, my name is John Su. Welcome to my homepage. I'm a senior technical director, technical artist, game engineer, VFX artist, and art-lover.

View My GitHub Profile

25 March 2024

Only Rotate Pawn When Moving

by John

Like shown in the above gif, there are instances that you want your character to stand still when it’s not moving, while rotating to camera direction when in motion.

I found the fairly easy way to implement it is:

Firstly, set the bUseControllerRotationYaw to false at some point (I opt to do it in the construction method.)

Then, utilize the bUseControllerDesiredRotation toggle of CharacterMovement! Basically when this is true the CharacterMovementComponent will align its X axis to match the camera direction.

Here’s my implementation:

    GetCharacterMovement()->bUseControllerDesiredRotation = (
		GetCharacterMovement()->GetLastUpdateVelocity().Length() > 1   // moving
		&& CaughtByPlayers.IsEmpty()	// not caught
		&& !IsValid(CaughtPlayer) // caught any player
	);

	if (CaughtPlayer)
	{
		const FRotator Rotation = UKismetMathLibrary::FindLookAtRotation(
			GetActorLocation(), CaughtPlayer->GetActorLocation()
		);

		const FRotator DesiredRotation = FMath::RInterpTo(GetActorRotation(), Rotation, DeltaTime, AlignToCaughtRate);

		GetCharacterMovement()->MoveUpdatedComponent(
			FVector::ZeroVector, DesiredRotation,
			/*bSweep*/ false );
	}

Note that when bUseControllerDesiredRotation is false, you can use GetCharacterMovement()->MoveUpdatedComponent to tweak the character’s rotation.

views: tags: UE - 3C