Smoothly Rotate Actor

Hello Community,

I’ve been looking around and I couldn’t seem to find a straight answer on how to perform what should be a simple task. I want to rotate an actor to face the player but I don’t want it to snap which is what it is currently doing. At the beginning of the function it just seems to snap immediately when it should be rotating smoothly to the location.

	if (ShouldTurn == true)
	{
		FVector MyLoc = this->GetActorLocation();
		FVector TargetLoc = PlayerCharacter->GetActorLocation();
		FVector Dir = (TargetLoc - MyLoc);
		Dir.Normalize();
		this->SetActorRotation(FMath::VInterpTo(MyLoc, Dir, Seconds, 10.0).Rotation());
		
	}

The VInterpTo should create a smooth transition over the course of 10 seconds if I understood the documentation correctly. In this case however it literally is instant which defeats the purpose of even using the FMath library function. Hopefully someone has a few ideas. Thank you in advance.

You can use FMath Lerp. What you want in your case is to lerp from your current Rotation to the new one.

if (ShouldTurn)
    {
             FVector MyLoc = GetActorLocation();
             FVector TargetLoc = PlayerCharacter->GetActorLocation();
             FVector Dir = (TargetLoc - MyLoc);
             Dir.Normalize();
             SetActorRotation(FMath::Lerp(GetActorRotation(),Dir.Rotation(),0.05f));
    }

Your useage of VInterpTo is wrong, you are trying to interp position and cast it to rotation but your MyLoc is not an orientation so it’s bound to fail.

Make sure you are doing the above code in the Tick() function too, a lerp needs to be ticked. So does VInterpTo.

Thanks this worked and yeah I was performing this under the Tick function. I could have sworn I read somewhere that the InterpTo method handled things smoothly but oh well. Anyways, thanks a bunch I really appreciate this.

You were right about the MyLoc orientation. Dunno how I made that slip.

Your code is focused on the location values of the actors, so that will not work out when dealing with rotation. Plus this way of interpolating the values will probably not result in a rotation motion.

Parts of your solution are still usable though. What I would do is:

Get the rotation of your actor and convert it into a normalized directional vector.

Get the dot product of your actors rotation vector and your normalized Dir variable. The output will be the angle between the actors rotation and the position of the player character. (in otherwords the delta angle between the two vectors - you might need to convert it from radians to degrees). In other words: original actor angle + delta angle = target angle.

Use this value to interpolate/lerp between the current rotation of the actor and the target angle and update the actors rotation accordingly. (Note: FRotator values are not vectors, they consist out of three angles yaw, pitch and roll).