Calculating angles in rotation?

Hello friends,

I am building an 2D interface (from the top down view) where the user sets waypoints by clicking the screen, and then having a character move from point A to B to C, etc.

I’ve been successful so far, but I would like my character to actually turn and directly face the waypoint when walking toward it.

How can this be achieved? I figure I’m suppose to manipulate the character’s rotation to face towards the next point. That would require calculating the angle(?) between point A and B, and then somehow using that angle on the characters rotation.

Thanks!

You can get the direction vector by subtracting B from A, then using Quaternion.LookRotation.

example:

Vector3 direction = target.position - transform.position;
transform.rotation = Quaternion.LookRotation(direction);
// and afterward, if you want to constrain the rotation to a particular axis- in this case Y:
transform.eulerAngles = new Vector3(0f, transform.eulerAngles.y, 0f);

Quaternion.SetLookRotation