Hello. I'm using Unity3d to create a simple turret simulation and I have run in to a problem.

I need to find the angle to an object from the turret in 3d space.

Up to this point I thought I could just take the (arc)trig values retrieved from certain perspectives (ie. looking towards the turret from X+ or Y+). Unity uses the left-hand coordinate system, with Y+ being upward and Z+ forward.

However in many tests the turret does not aim directly at the object. I have determined that the problem is the length of my direction vector. After being normalized it is indeed 1 unit in length but this is in 3d space which means that length is longer than comparable 2d vectors (having the same position from an orthographic perspective along an axis). This fact is making all of my inverse trig functions give me incorrect values for this problem.

So my question is:

Can somebody provide me with a mathematical solution and explanation of it (for my own learning)? I can work out the code sure enough I just do not know the mathematics to solve it.

Crap. I've actually solved this before using trig just thought I could do it again. The code for it is around here somewhere I just haven't looked for it... just decided to use the transform.lookat() function and copy values. Thanks.


This is physics and has to do, in my opinion, with OOP more or less like physical.
The solution I think is θ' = π - θ because I should have taken PP' instead of P'P.