Hi there,
I have seen this question asked in different ways on here before, but none help me with the problem I am having.
The project I am working on involves rotating an object until it is facing the desired angle, x, y and z.
I need to compare two Vector 3 angles, one is the desired angle say (0, 90, 0), the other is the constantly changing spinning angle of my object.
The problem I have is that when I use any method I have found to compare the two angles, I always get completely unpredictable results. Results that change when the rotation is represented closer to 360. So say my desired angle is 5 degrees. If the angle currently = 45 it will seem to the code much closer than an angle of 359, even though it is in reality much closer to the eye.
I’d really appreciate any help you can give. I currently have this
void Start () {
lockAngle = new Vector3(0, 90, 0);
}
void Update () {
Vector3 vectorOne = lockAngle;
Vector3 vectorTwo = transform.eulerAngles;
float angle = Vector3.Angle( vectorTwo, vectorOne);
Debug.Log(vectorOne + " : " + vectorTwo + " = " + angle);
}
Hi pixelLover,
There is a huge thread on this somewhere on the forum, but I can’t find it 
I can’t explain the details(at work), but I can share some code that should help you along.
...
// fwd/up changed because of gfx supplied.
float side = AngleDir(Vector3.right, pointDir, Vector3.forward);
float angle = Vector3.Angle(Vector3.right, pointDir);
angle = side > 0 ? angle : 360 - angle;
...
private float AngleDir(Vector3 fwd, Vector3 targetDir, Vector3 up) {
Vector3 perp = Vector3.Cross(fwd, targetDir);
float dir = Vector3.Dot(perp, up);
if(dir > 0f) {
return 1f;
}
else if(dir < 0f) {
return -1f;
}
else {
return 0f;
}
}
This example is quite specialized, but you should get the gist of it. The trick is knowing which side of the angle you need to look at…
EulerAngles are the wrong inputs. Vector2.Angle expects to see a pair of “direction arrows,” like the red/green/blue arrows on the movement widget. Take a look at the official docs: Unity - Scripting API: Vector3.Angle
They show how to compare your personal blue forward arrow, with an arrow from you to a target, to get the viewing angle to it.
Another example, suppose you wanted to see how level you are, by comparing your personal up arrow with real up. Use: Vector3.Angle( transform.up, Vector3.up );
. You may need to read about local vs. world coordinates (like, “how and why is trans.up and V3.up different?”) That stuff can be a little confusing.
Hey,
I’ve actually got it working now with this method
Vector3 vectorOne = lockAngle;
Vector3 vectorTwo = transform.eulerAngles;
float diffX = Mathf.DeltaAngle(vectorOne.x, vectorTwo.x);
float diffY = Mathf.DeltaAngle(vectorOne.y, vectorTwo.y);
float diffZ = Mathf.DeltaAngle(vectorOne.z, vectorTwo.z);
float difference = Mathf.Abs(diffX) + Mathf.Abs(diffY) + Mathf.Abs(diffZ);
if(difference < 20) {
// You did it - Add some code here
} else {
// Not aligned - Add other code here
}
Probably by no means the best way though, I will have a look at the code you posted. At least with the method above it’s easy to choose to lock to one angle or all 3.