I am following this tutorial by Sebastian Lague: Introduction to Game Development (E23: stealth game 1/3) - YouTube
At 11:58 in the video (The timemark is in the link) he is first getting the direction of the vector towards the target he wants to rotate towards, by subtracting the target position by transform.position. That part I understand, but what I would like to understand better is how he then gets the desired angle by taking the tangent of the target’s x and z values. How does this give the wanted rotation angle?
Any help would be much appreciated!
He does not take the tangent of x and y but he takes the “arctan” of “y/x”. The special atan2 method does the same but in addition it takes the quadrants into account and applies the proper 90° rotations to get a 360° result
The “tan” method takes an angle in radians and calulates the length of the tangent on the unit circle. The atan method does the reverse. You pass in the length of the tangent and it returns the angle. This only works properly for the first (main) quadrant of the coordinate system. Though since atan2 gets y and x seperately it can analyse the sign of x and y and apply the quadrant corrections. I’m not sure what else we can say here. It’s just basic trigonometry. By dividing y by x we get the length of the tangent on the unit circle y / x == tan(a) / 1