Problems with Vector3.Angle... Anyone have any ideas?

Hey, all,

I’m working on a pretty maths heavy game, which involves loads of calculations between vectors, but I’m having real issues with Vector3.Angle.

Every other frame, I get the same result as I got the previous frame, but as a negative version of the prior, like so:

Frame 1:
Angle = 180.5
Frame 2:
Angle = -0.5
Frame 3:
Angle = 180.5
Frame 4:
Angle = -0.5

At first I thought it was my code (obviously), but then I tried the following:

Angle = Vector3.Angle ( Vector3.up, Vector3.left );

Which gave the following results:

Frame 1:
Angle = 90
Frame 2:
Angle = 0;
Frame 3:
Angle = 90;
Frame 4:
Angle = 0;

This isn’t really much use to me, as I really need to know what the actual angle is or my calculations are all out.

Is this a bug? Or something I’m missing?

Anyone have any ideas how to get around it?

Cheers,

SB

Looks like each frame you’re rotating Angle exactly 90 degrees :slight_smile:

It suggests to me (though I don’t see how) that Vector3.up etc is relative to Angle present orientation, even though I didn’t think its meant to do that?

Nope, I’m no help at all, I’m confused :slight_smile:

Can you post the whole script? Vector3.Angle isn’t supposed to be dependent on any kind of game state, so it would be very strange for it to behave like this.

You don’t actually need the whole script! Doing the following:

function Update()
{
     Debug.Log ("Angle = "+Vector3.Angle (Vector3.forward, Vector3.left) );
}

demonstrates exactly what I’m talking about. If you run it, you get:

Angle = 0;
Angle = 90;
Angle = 0;
Angle = 90;
etc…

Obviously this isn’t the code in my game, but I’d taken my script apart to a point where I decided to run this as a test, and got the above results. Really weird, and very difficult to work around - it’s messing up my compass! :frowning:

Have you tried below just to see if its the syntax?

function Update()
{
     Debug.Log ("Angle = "+Vector3.Angle (Vector3(0,0,1), Vector3(-1,0,0) );
}

Also, I dont see a Vector3.left in the script documentation. I am assuming its just not mentioned but does exist.

Yes, strictly speaking, Vector3.left is a relic that isn’t supposed to be used (which is why it is undocumented). I’m running this by the dev team to see if using Vector3.left could be causing the problem.

	void Start () 
    {
        Debug.Log(Vector3.Angle(new Vector3(0, 0, 0), new Vector3(0, 10, 10)));
        Debug.Log(Vector3.Angle(new Vector3(0, 0, 0), new Vector3(10, 0, 10)));
        Debug.Log(Vector3.Angle(new Vector3(0, 0, 0), new Vector3(10, 10, 0)));
        Debug.Log("next");
		Debug.Log(Vector3.Angle(new Vector3(0, 0, 0), new Vector3(10, 10, 0)));
        Debug.Log(Vector3.Angle(new Vector3(0, 0, 0), new Vector3(10, 0, 0)));
        Debug.Log(Vector3.Angle(new Vector3(0, 0, 0), new Vector3(10, -10, 0)));
        Debug.Log("next");
		Debug.Log(Vector3.Angle(new Vector3(1, 0, 0), new Vector3(11, 10, 0)));
		Debug.Log("next");
        Debug.Log(Vector3.Angle(new Vector3(5, 6, 7), new Vector3(10, 10, 0)));
        Debug.Log(Vector3.Angle(new Vector3(5, 6, 7), new Vector3(10, 0, 0)));
        Debug.Log(Vector3.Angle(new Vector3(5, 6, 7), new Vector3(10, -10, 0)));


	}

and its output
90
90
90
next
90
90
90
next
42.27369 :wink:
next
42.13
61
93

I am guessing this is because angle is a canonical Euler angle and its entering Gimbal lock so you get 90 when you would expect 45.

Angle could use better documentation. Note I did not test this in Unity iPhone

it is quite simpe if you know the conditions clearly.
This image illustrates it (i am using x/z axis):

the code would be like:

var A:Vector3 = Vector3( 0.0, 0.0, 1.0 ) // x,y,z
var B:Vector3 = Vector3( 1.0, 0.0, 1.0 ) // x,y,z
var angle:float = Vector3.Angle( A,B );
    // angle == 45

Debug.DrawLine ( Vector3.zero, A, color.yellow ); 
Debug.DrawLine ( Vector3.zero, B, color.yellow );

so what we do here is set the Vector A along the z-axis and the Vector B along the x/z plane.

The important thing to understand is that (if i’m not mixed up) Vector3.Angle always calculates the angle from the Vector3.zero to the actual Points.