How to make an actor rotate to look in a certain direction in C++

If I have one actor (A) and another actor (B) and I want that, at certain point of the game, A rotates to look in the direction where B is, how can I do it? It is not necesary to interpolate, the change of rotation can be instant and I have access to both actors and their properties

Barring any built-in UE4 commands that already exist (that I can’t come up with off the top of my head at the moment), you could:

  1. Find the vector between actor A’s position and actor B’s position ( FacingVector )
  2. Create a FRotator from the vector ( FRotator FacingRotator = FacingVector.Rotation() )
  3. Set actor A’s rotation with FacingRotator ( ActorA->SetActorRotation(FacingRotator, ETeleportType::None) )

Also, to avoid any pitching up/down (you only care about the XY plane), make sure the Z of FacingVector is 0 before converting to FRotator. Or simply set FacingRotator’s pitch to 0 before applying it to the actor.

Hopefully, that’ll get you what you want. If anyone else knows a built-in way, chime in.

Most people use this:
UKismetMathLibrary::FindLookAtRotation(sourcelocation, targetlocation)…ion/index.html

Once you have the rotation, you can use Lerp or you can just add it to your self using addrelativerotation or just set the rotation manually.

manual method if you need to specify the up vector:

FVector newForward = Target - Source;

const FVector WorldUp(0.0f, 0.0f, 1.0f);

FVector newRight = FVector::Cross(toTarget, WorldUp);
FVector newUp = FVector::Cross(newRight, newForward);

return FTransform(newForward, newRight, newUp, Source);

Even better. I knew there had to be a built-in way.