Download

Actor rotation snaps from -180 to 180 when using Lerp function

Hey everyone!

So I’ve been having this problem where my character’s rotation goes from -180 to 180 (the long way around - clockwise) Instead of taking the short path (counter clockwise) and it looks really weird because I’m trying to use lerp on the rotation so that the character won’t instantly rotate. And it works for every angle except when going from -180 to 180

This is the code I’m using to rotate my character:



            FHitResult TraceHitResult;
            PC->GetHitResultUnderCursor(ECC_Visibility, true, TraceHitResult);
            FVector CursorFV = TraceHitResult.ImpactNormal;
            FRotator CursorRotation = CursorFV.Rotation();

            FRotator ActorRotation = GetActorRotation();
            ActorRotation.Yaw = FMath::Lerp<float>(ActorRotation.Yaw, CursorRotation.Yaw, 0.1f);
            SetActorRotation(ActorRotation);



And this is the result.

It’s also worth noting that this doesn’t happen if I don’t lerp the yaw value, something like this:



            FHitResult TraceHitResult;
            PC->GetHitResultUnderCursor(ECC_Visibility, true, TraceHitResult);
            FVector CursorFV = TraceHitResult.ImpactNormal;
            FRotator CursorRotation = CursorFV.Rotation();

            FRotator ActorRotation = GetActorRotation();
            ActorRotation.Yaw = CursorRotation.Yaw;
            SetActorRotation(ActorRotation);



Here’s the result.

This way, the rotation snap is much more noticeable (as seen here)](https://youtu.be/YSEzJcMT1UY) but it wont go the long way around.

I’d appreciate any help, tips or suggestions on how to improve my character’s rotation, especifically on how to avoid my character from going from -180 to 180 clockwise, since it looks so silly.

This should be an easy fix, for Rotators I dont use Lerp for this exactly reason, insted use FMath::RInterpTo, it will Lerp the whole rotator and take in account of the Euler rotation then you can get get the yaw from the return Rotator.

Or you could use RLerp and get the yaw from that returned Rotator

Oh thank you so much, that was indeed an easy fix, cheers!