- Home /
Quaternion.Angle and 360 degrees
Hi!
I need to receive a angle between rotations of two objects from 0 to 360 degrees. However the function "Quaternion.Angle" produces value from 0 to 180.
public GameObject Object1, Object2;
void Update () {
Object1.transform.Rotate (Vector3.up * 10 * Time.deltaTime);
float angle = Quaternion.Angle (Object1.transform.rotation,
Object2.transform.rotation);
Debug.Log (angle);
}
Thanks.
Is this essentially a 2-d problem? That is, does the world have a fixed up vector, and is the angle you're looking for relative to that up vector?
Answer by mrde · Jan 27, 2011 at 12:08 PM
you can fix one time dot product of these two GameObject forward dot product Vector3 up = dot(object1.transform.forward, object2.transform.forward); and ever@ interpolate state compare direction dot with up (in normalize mode), it must be same vector or invert vector, if dot is invert vector that mean (angle = 360 - angle)
void Start() { Vector3 right = object2.transform.TransformDirection(Vector3.right); Vector3 up = Vector3.dor(object2.transform.TransformDirection(Vector3.forward), right); }
void Update() { Object1.transform.Rotate (Vector3.up 10 Time.deltaTime); Vector3 newup = Vector3.dor(object2.transform.TransformDirection(Vector3.forward), object1.transform.TransformDirection(Vector3.forward)); float angle = Quaternion.Angle (Object1.transform.rotation, Object2.transform.rotation); if (up.normalized != newup.normalized) { angle = 360 - angle; } }
not tested.
Did you mean TransformDirection()? Also, you don't have to write 'Vector3 right = object2.transform.TransformDirection(Vector3.right);'; you can just write 'Vector3 right = object2.transform.right;'.
Your answer
Follow this Question
Related Questions
Rotate floor plane in-game via C# script 1 Answer
Rotation according to gravity 1 Answer
Set the rotation reference to an direction 0 Answers