Why are the operations not equivalent (rotation)?

Why are the following operations not equivalent?

const auto TargetRotation = UKismetMathLibrary::FindLookAtRotation(GetActorLocation(), ToLocation);
SetActorRotation(TargetRotation);

and:

const auto TargetRotation = UKismetMathLibrary::FindLookAtRotation(GetActorLocation(), ToLocation);
AddActorWorldRotation(TargetRotation - GetActorRotation());

In the first case, the actor will always look at ‘ToLocation’.

In the second case, the actor sometimes starts spinning wildly (for example, if actor location == {0,0,0}, and ToVector == {0,0,100})

They can’t be the same

AddActorWorldRotation(TargetRotation - GetActorRotation())

depends entirely on what rotation the actor happens to have before the transform.

The first case is:

int A = 4;
int B = 7;
A = B;

The second case is:

int A = 4;
int B = 7;
A += B - A;

That is, these cases should be equivalent if the rotation works the way I think.

Ah, they don’t work like that.

Basically, UE decides for itself that -180 might really be +180, that’s the problem.

If you’re setting rotation, this will always be a problem. There are two ways around it

  1. Use quaternions

  2. Only ever add local rotation