- Home /
Quaternion.Angle always returns zero
I have object that runs towards a set destination (set through right-click) and the object arrives perfectly but I also have a desired rotation being stored when the user holds down the right mouse button and picks a rotation (a graphical arrow appears to show rotation). Everything works until the object reaches its destination when I want the object to rotate to face the desired rotation. I have a check method to see if the rotation difference between the object and desired but it always returns zero, even if I manually rotate the object (just around the y-axis), no matter what it always comes back as a zero difference.
bool CheckAngleDifference(Quaternion a1, Quaternion a2,float diffAmnt)
{
print (a1 + " - " + a2);
print ("angle diff: " +Quaternion.Angle (a1, a2));
if (Mathf.Abs (Quaternion.Angle (a1, a2)) > diffAmnt)
return true;
return false;
}
I have made this work using: Quaternion.Angle (transform.rotation, (Quaternion.LookRotation (newDest - transform.position, Vector3.up)))
but in this case the positions are the same since it has arrived at its destination. Any idea why Quaternion.Angle keeps coming back as zero and how can I get the object to face the desired rotation after arriving at its destination. Big thanks in advance for any hep.
it always comes back as a zero difference
Is this according to your print statement?
You cant debug Quaternion.Angle (a1, a2)
via the if statement as it adds in other factors. Your print throws out 0 every time?
How about if you use
float diffAngle = Quaternion.Angle (a1, a2);
print (diffAngle.ToString());
I still get zero every time, no matter the angles.
So what does the print for a1 and a2 say?
Your code works for me.
All I can suggest is that you are incorrectly setting up your quaternions.
Are you sure you aren't requiring Vector3.Angle ins$$anonymous$$d?
When the object (character) arrives at its destination I get a1 = (0.0,-1.0,0.0,-.02) and a2 = (0.0,-174.1,0.0,0.0) and the whole time angle difference is zero. So you when run that you get a result other than zero? Very strange (thanks for the responses btw).
Here, I placed this code on an empty object then created 2 cubes, which I dragged into the transform entries of the script. Rotating the cubes then pressing G prints the result.
using UnityEngine;
using System.Collections;
public class AngleTest : $$anonymous$$onoBehaviour {
Quaternion q1 = Quaternion.identity;
Quaternion q2 = Quaternion.identity;
float diff = 0;
public Transform t1;
public Transform t2;
void Update () {
q1 = t1.rotation;
q2 = t2.rotation;
if(Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.G))
{
CheckAngleDifference(q1,q2,diff);
}
}
bool CheckAngleDifference(Quaternion a1, Quaternion a2, float diffAmnt)
{
print (a1 + " - " + a2);
print ("angle diff: " +Quaternion.Angle (a1, a2));
if ($$anonymous$$athf.Abs (Quaternion.Angle (a1, a2)) > diffAmnt)
return true;
return false;
}
}
Yea, I get a non-zero result as well. Obviously the problem is somewhere else. When I get this figured out I'll post the results. Thanks again!
Answer by Amir1360 · Jan 14 at 02:42 PM
Please refer to this:
A more accurate function to calculate the angle between two rotations
Your answer
Follow this Question
Related Questions
Issue finding an angle with trigonometry 1 Answer
Clamp Rotation of an object depending of the Rotation of an other one 0 Answers
How to check object "rotated" X degrees 1 Answer
Setting rotation Angle / rotation question 1 Answer
I want to rotate object away or towards camera around x-axis 1 Answer