This function gives you the angle in degrees from one vector to another. Problem is, it doesn't account for one continuous direction of rotation. If I rotate one of the vectors all the way around a given axis, the angle between the two will increase to 180, then decrease back to zero. I want to get a representation of an angle that will range from 0 to 359. How do I do that?
You can use the code in this thread to determine whether one vector is to the left or the right of another, given a third vector specifying the upward direction. If the vector is to the left of the reference vector, then subtract the result of Vector3.Angle from 360º to get a continuous angle:- Code (csharp): function ContAngle(fwd: Vector3, targetDir: Vector3, upDir: Vector3) { var angle = Vector3.Angle(fwd, targetDir); //The AngleDir function is the one from the other thread. if (AngleDir(fwd, targetDir, upDir) == -1) { return 360 - angle; } else { return angle; } }
I do it following the mathematic definition of a scalar product between two vectors (it works to me). float angle = Mathf.Acos(Vector3.Dot(vector1, vector2)); Edit: now I understand your doubt... I guess you could use an auxiliar vector defined orthogonal to vector1 and vector2 (let's call it vector3), and then use two Dot products. if Vector3.Dot(vector1, vector3) > 0, angle is between 0 and 180. if Vector3.Dot(vector1, vector3) < 0, angle is between 180 and 360. Redit: That thing I said was stupid because the dot product would be zero because of my definition of vector3. The idea would be having a vector3 coplanar with vector1 and vector2. As it seems I'm not in my most acurate moment, I let you defining vector3 as you want... The idea of the two Dot products is still valid, while you define vector3 properly.