I have an object A in a 3D world (ortho view). I want object A to rotate along the X axis to face object B. Object A must not change on the y and z axis.
So I knew I was complicating things far too much. So I took it down to the basics. This is my problem in its simplest form: http://i.imgur.com/ImcmpxQ.png
From the positions we can calculate the sides of the triangle with the code
float z = Vector3.Distance(objectB.transform.position, objectA.transform.position);
float x = objectB.transform.position.x - objectA.transform.position.x;
float y = objectB.transform.position.y - objectB.transform.position.y;
then we use the equation [1] to calculate the angle: float angle = Mathf.Acos((Mathf.Pow(y, 2.0f) + Mathf.Pow(z, 2.0f) - Mathf.Pow(x, 2.0f) / 2 * y * z)); BUT [Mathf.Acos][2] gives the number back in Radiants. So to convert to Degrees do: angle *= Mathf.Rad2Deg; However, if we were to put this into Rotate() right now, it would rotate the wrong way if ObjectB is on the right side of ObjectA, so we have to make the angle a negative if it is on the right hand side (ie if x position of ObjectB is larger than ObjectA’s) if (x > 0) angle = -angle; then finally, put this angle into the simple Rotate() function, rotating on the x axis objectA.transform.Rotate(Vector3.forward, angle); Here is the thing in its entirety for easy copy and pasting for anyone looking this up: float z = Vector3.Distance(objectB.transform.position, objectA.transform.position); float x = objectB.transform.position.x - objectA.transform.position.x; float y = objectB.transform.position.y - objectB.transform.position.y; float angle = Mathf.Acos((Mathf.Pow(y, 2.0f) + Mathf.Pow(z, 2.0f) - Mathf.Pow(x, 2.0f) / 2 * y * z)); angle *= Mathf.Rad2Deg; if (x > 0) angle = -angle; objectA.transform.Rotate(Vector3.forward, angle); [1]: Triangle Calculator [2]: Unity - Scripting API: Mathf.Acos