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.

So, I was in a similar programm assignement not to long ago, and these 2 links helped out for me. One is to help me understand the solution in the form of vector math, and the other help me verify my answers.

http://www.mathportal.org/calculators/matrices-calculators/vector-calculator.php
https://www.studypug.com/algebra-help/vectors/direction-angle-of-a-vector

I know, tempting to just go and hack at it, until your canon ball start defying the Newton's law and gravity.

turretangle.png

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.