Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Getting the angle between 2 objects

Discussion in 'Scripting' started by chips, Oct 6, 2008.

  1. chips

    chips

    Joined:
    Nov 30, 2007
    Posts:
    138
    Hello there, I'm having some difficulty to get the angle between two objects and know if the target object is it at my right or my left...
    If I use this, (the code down here) I can get an angle difference, but only in 180 degrees...

    Code (csharp):
    1.  
    2.         var relativePos = nextPoint - transform.position;
    3.         rotation = Quaternion.LookRotation(relativePos);
    4.            
    5.         var forward = transform.forward;
    6.         var angle = Vector3.Angle(relativePos, forward);
    7.  
    8.         //angle and Quaternion.Angle(transform.rotation, rotation) brings me the same result. but still I can't see witch side the object are...
    Hope anyone can help.
    THANKS!
     
  2. Talzor

    Talzor

    Joined:
    May 30, 2006
    Posts:
    197
    The trick is to check the cross product between the 2 vectors. In your case forward and relativePos (which isn't actually a position but a direction :D). The cross product of two vectors are a new vector perpendicular to the plane defined by the vectors, so now you just need to check if the y component of the cross product is positive or negative (I.e. is the vector pointing up or down). Up = nextPoint is on the right, Down = to the left (See code snippet)

    BTW: Obviously I don't know what your doing, but if the
    Code (csharp):
    1. rotation = Quaternion.LookRotation(relativePos)
    line is actually
    Code (csharp):
    1. transform.rotation = Quaternion.LookRotation(relativePos)
    I suggest that you move it after the rest of the code, otherwise the angle between the 2 object are always going to be 0 (since you've just rotate to face the other object directly).

    Code (csharp):
    1.  
    2. var relativePos = nextPoint - transform.position;
    3.          
    4. var forward = transform.forward;
    5. var angle = Vector3.Angle(relativePos, forward);
    6. if (Vector3.Cross(forward, relativePos).y < 0) {
    7.   //Do left stuff
    8. }
    9. else {
    10.   //Do right stuff
    11. }
    12.  
    13. rotation = Quaternion.LookRotation(relativePos);
    14.  
     
    Novack and SkelleRoznik like this.
  3. chips

    chips

    Joined:
    Nov 30, 2007
    Posts:
    138
    I worked Talzor!!!
    I Thank you very much!!!
    I owe you one! :D
     
  4. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    I prefer:
    var something : Transform; // position of the other object

    var p : Vector3 = transform.InverseTransformPoint(something.position);

    Now p is the position of something in my local coordinate space. If p.y > 0, it's above me. If p.x < 0 it's to my left. If p.z < 0 it's behind me. Very convenient for writing any kind of AI code.
     
  5. crazyKnight

    crazyKnight

    Joined:
    Nov 22, 2010
    Posts:
    55
    @Talzor : thanks a lot your code works like charm ....