I know this works, but I don't know why it works. Can someone explain?

Okay, so I know that I can have an object rotate towards another object by this code, but I don’t know why it works and I can’t seem to just leave it alone and accept that it does what it does without knowing why and where my error in thought is.

Vector3 direction = player.position - transform.position;
transform.rotation = Quaternion.slerp(transform.rotation, Quaternion.LookRotation(direction), 1);

If I minus the players coordinates, let’s say they’re 5,1,7 from the objects coordinates, let’s say they’re 3,5,8 I get
-2,4,1, which is not actually where my player object is… Why then does this word whereas just telling the program to look at the player gives strange results? I’m obviously missing something here.

So first things first. A vector is not a point in space but rather a point and a direction. When it’s used as a point in space it’s in actuality a vector pointing from the world origin to the point in space. So - subtracting a vector from another vector gives you a vector that points from one to the other. In this case, pointing from transform to player.

Slerp is short for spherical interpolation. Lerp is short for linear interpolation. Both operate in a manner of Slerp(A, B, P) which interpolates from A to B by the percentage amount of P. So 0 is A and 1 is B and 0.5 is halfway between A and B. Therefore, you could simplify your code and remove the Slerp entirely.

transform.rotation = Quaternion.LookRotation(direction);

With all of those things said - you can actually do what you mentioned

transform.LookAt(player);
1 Like

When you subtract a position to another, you get a directional vector.

Kelso explained it well but since it took me and my artistic skills 10 mins to come up with that drawing, there’s no way im letting it go to waste.

2 Likes

This is great! Thank you so much! It’s nice to know where my understanding of vector was flawed.

Thanks again.