yAxis rotation parent object.

Line 10 is your problem. Subtracting one axis from another does not give an angle. In fact, it is mathematically meaningless.

Basic trigonometry tells us that the tangent of an angle is the opposite side over the adjacent. Remember TANOA from algebra in school?

The function I suggested is called Mathf.Atan2() for a reason: it takes 2 coordinates and returns you the arctangent, which is the inverse of the tangent. In other words, it maps from two orthogonal axis to an angle.

This means that the expression Mathf.Atan2(xAxis,zAxis) * Mathf.Rad2Deg will give you an angle based on the relative magnitude and direction of the two inputs.

You could then use that angle as your Rotate, perhaps with a 90-degree or 180-degree or other offset/negation.

Generally when you HAVE an angle, you don’t use .Rotate(), which is a relative command.

Instead when you have the angle, you just set the angle directly with a construct like this:

transform.rotation = Quaternion.Euler( 0, angleThatICalculatedAbove, 0);
2 Likes