I have following line running in Tick()
:
SetActorRotation(FMath::Lerp(GetActorRotation(), Direction.Rotation(), Speed));
Where Direction
is a normalized FVector
and Speed
is a float between 0 and 1.
Im expecting this to interpolate the actors rotation towards Direction
, but what happens is that the new direction is simply Direction * Speed
, no matter how many frames passes.
Im sure that it is running in Tick()
, Direction
and Speed
where constant when testing. I can’t figure out what’s wrong.
If you want to interpolate between updated rotation and targeted rotation then use RInterpTo:
SetActorRotation(FMath::RInterpTo(GetActorRotation(), Direction.Rotation(), DeltaTime, Speed));
If you use Lerp, you just can’t use values that change as parameters. So you can’t use GetActorRotation() directly, but you must use it’s initial value. So save it in a variable (outside the Tick function), and then use that value as the first parameter.
Edit: Also, the third parameter in Lerp should be Alpha, which must go from 0 → 1 as time goes by. If your speed has a value greater than 1.0, then lerp will always return Direction.Rotation().
I figured out the problem wasn’t caused by the code, but by an overwrite of bUseControllerRotationYaw i had accidently made in the editor. Lerp and RInterpTo results in the same behaviour if speed in Lerp is multiplied by DeltaTime. Using RInterpTo might be preferable though, because it is cleaner and not a “hack”.