I am attempting to rotate a 3D object on its Y-axis towards a target it is looking at. I have always struggled with rotations so I would like to try to avoid using Unity's built in functions, i.e transform.LookAt() in order to get used to using rotations. Gotta master the basics first, right?
Vector3 currentOrientation = transform.rotation.eulerAngles; Vector3 directionVector3 = transform.position - target.transform.position; float angle = Mathf.Atan2(directionVector3.z, directionVector3.x) * Mathf.Rad2Deg; currentOrientation.y =angle; transform.rotation = Quaternion.Euler(currentOrientation);
However when I do run this, the character is not looking in the correct direction at all!
Here is a screen shot with the character's parameters
The target object belongs in a hierarchy, where it's position in world space is (150,0,500)
Does atan2 somehow behave differently in 3D space vs 2D space? I ask because I've made 2D games where I would just use atan2(y,x) for a character's rotation.