# How do i check the angles between 2 positions?

Discussion in 'Scripting' started by gilson, Sep 21, 2010.

1. ### gilson

Joined:
Sep 13, 2010
Posts:
36
For instance, i have a object facing a direction, i want it to rotate towards his right, until he have rotated 45 degrees and the some to the other side, like a patrol.

I tried something like this:

Code (csharp):
1.
2. transform.Rotate(0,Time.deltaTime * 5, 0);
3. Vector3.Angle(position.position.forward, transform.forward));
4.
where 'position' is my initial and transform.forward is where im looking at since im rotating.

But what he does is check the angle between the world front Z and not the position of my object. Did i make myself clear? hahaha

2. ### Jesse Anders

Joined:
Apr 5, 2008
Posts:
2,857
Well, I'm not exactly sure what you're intending for that code to do, but the second line is basically a no-op (since you're ignoring the return value).

Regarding your question, computing the angle between two 'positions' isn't really well defined; you can consider them to be vectors relative to the origin and compute the angle between the vectors, but that's not an operation that's useful very often (at least not in my experience).

But, I think what you probably mean by 'position' here is 'orientation', and you want to determine how far the agent has rotated relative to its initial orientation. If so, I think you have the right idea; you just need to do something with the return value of Angle(). (Note that Angle() returns the unsigned angle between the two vectors, but, you can easily determine the sign by taking the dot product of the current forward vector and the original transform's right or left vector.)

3. ### gilson

Joined:
Sep 13, 2010
Posts:
36
Yes, thats what i need. I need to know how much it has rotate from its initial position.

I was running some basic tests:

Code (csharp):
1. function Update ()
2. {
3.     initialPosition = transform.position;
4.     angle = Vector3.Angle(initialPosition.forward, transform.forward);
5.     print(angle);
6.
7. }
If my object is turned towards the Z angle, with Y = 0. The function returns 0, but if my object is with Y = 45 for instance, it returns 45. Shouldnt the funtion return the angle between my starting coordinates? I guess that would return 0 no matter wich coordinates it would starts and would increase as i rotated the object.

4. ### gilson

Joined:
Sep 13, 2010
Posts:
36
It appears that the variable initialPosition doesnt have my current position and i guess it should have. :/

Any ideas?

5. ### Jesse Anders

Joined:
Apr 5, 2008
Posts:
2,857
I use C#, so I can't comment directly on the code above, but in C# at least the equivalent code wouldn't compile (and if it compiles in UnityScript, I'm guessing it doesn't do what you're expecting it to do). Unless I'm missing something, you're trying to access the non-static member field Vector3.forward, which doesn't exist.

I think part of the problem is that you're misusing the term 'position'. A position is a point in space; it seems that what you're wanting to know is, how far has the object rotated? That is, how much has its orientation changed (not its position).

To determine this, you'll probably want to save the object's initial forward vector rather than its position. Then, compare its current forward vector to the initial forward vector to determine how far it's rotated.

6. ### AkilaeTribe

Joined:
Jul 4, 2010
Posts:
1,149
I can't bear it anymore :evil:

The problem is that you always put initialPosition as the current position. A movement has a beginning and an end. initialPosition should only be refreshed when the movement BEGINS, not ALWAYS. It's like wanting to only run one kilometer, but if you always declare your starting point is your current position, calculating your travelled distance will always give you ZERO.

Code (csharp):
1. function Start ()
2. {
3.     RotateAngle (45.0);
4. }
5.
6. function RotateAngle (angle : float)
7. {
8.     var initialForward : Vector3 = transform.forward;
9.
10.     while (true)
11.     {
12.         var currentForward : Vector3 = transform.forward;
13.         var angleAbs : float = Mathf.Abs (angle);
14.         var angleBetween : float = Vector3.Angle (initialForward, currentForward);
15.
16.         if (angleBetween < angleAbs)
17.             transform.Rotate (0.0, 5.0 * Time.deltaTime, 0.0);
18.         else
19.             break;
20.     }
21. }

7. ### Jesse Anders

Joined:
Apr 5, 2008
Posts:
2,857
Hehe...I hadn't even gotten to the program logic yet. I'm still trying to figure out what 'initialPosition.forward' is supposed to do

8. ### AkilaeTribe

Joined:
Jul 4, 2010
Posts:
1,149
It does nothing, so, like useless fat chickens, it should go to the garbage can :twisted:

9. ### gilson

Joined:
Sep 13, 2010
Posts:
36
Code (csharp):
1.
2. function Update ()
3. {
4. initialPosition = transform.position;
5. angle = Vector3.Angle(initialPosition.forward, transform.forward);
6. print(angle);
7.
8. }
9.
This logic was just to show that Angle returned me the initial angle of my object, wich was not the idea, since the starting angle should be always 0. The problem was as Jesse was saying, that i sould get transform.forward as my initialAngle and not transform.position!

After i changed that it works

Thanks for the enlightnment...[/code]

unityunity