Search Unity

Getting the direction from one object to another

Discussion in 'Scripting' started by roBurky_legacy, Nov 18, 2009.

  1. roBurky_legacy

    roBurky_legacy

    Joined:
    Nov 7, 2009
    Posts:
    17
    I've looked through the scripting guide, and I'm not sure what function I want for this. Vector3.Angle sounded like it was the one, but it doesn't behave as I was expecting.

    I'm making a game on a 2D plane, using C#. I have an enemy missile object that should check in what direction the player is from it, compare that with the direction the missile is facing, if the player is to the left, turn left, if the player is to the right, turn right.

    It's just the bit about finding the direction from the missile to the player I'm unsure about.

    Thanks for any help you can give.
     
  2. nooB

    nooB

    Joined:
    Oct 4, 2009
    Posts:
    20
    You can try this
    Quaternion.LookRotation();
     
  3. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Quaternion.LookRotation is a good choice if you want the missile to point directly at the target object at all times. However, if you want to turn the missile incrementally to try to point towards the target, you will indeed need to know which way to turn. It's actually more subtle than you might think, but there is an explanation and some sample code in this thread.
     
  4. roBurky_legacy

    roBurky_legacy

    Joined:
    Nov 7, 2009
    Posts:
    17
    Thanks, andeeee. I was able to get what I needed out of that sample code.
     
  5. roBurky_legacy

    roBurky_legacy

    Joined:
    Nov 7, 2009
    Posts:
    17
    Hello,

    In working on another piece of code, I have bumped into my original problem, which I still haven't actually solved.

    I have the Vector3 positions of two objects. They have the same z value, so are effectively on the same 2D plane. I want to find the angle from one to the other, in degrees.

    I'm not wanting to rotate an object to point directly at the other, so I don't think the lookAt() function is what I want. I just want to know the angle, in degrees.

    Can anyone help?
     
  6. Gaiyamato

    Gaiyamato

    Joined:
    Nov 19, 2009
    Posts:
    95
  7. roBurky_legacy

    roBurky_legacy

    Joined:
    Nov 7, 2009
    Posts:
    17
    Thanks for replying.

    I can't quite figure out what that function does from the description, or how to use it.
     
  8. Gaiyamato

    Gaiyamato

    Joined:
    Nov 19, 2009
    Posts:
    95
    I'm not sure. The Unity docs are incredibly vague on a lot of things. That is, where they are not out of date and/or blank completely.

    I have found the odd use for it in this sort of instance by fiddling with it and doing random stuff until something worked though.

    What about line casting between the objects and finding the angle/direction from the line cast itself?
     
  9. Charles Hinshaw

    Charles Hinshaw

    Joined:
    Feb 6, 2008
    Posts:
    1,070
    Quaternion.ToAngleAxis (out angle : float, out axis : Vector3), when used in a context like transform.rotation.ToAngleAxis(angle, axis), will set the values for angle and axis so that the represent transform.rotation in the form of angle degrees around axis.
     
  10. Gaiyamato

    Gaiyamato

    Joined:
    Nov 19, 2009
    Posts:
    95
    That tells us nothing of use.
    From what points does it calculate the angle? When we get the angle how can we know what direction it would point us to if used?
     
  11. Charles Hinshaw

    Charles Hinshaw

    Joined:
    Feb 6, 2008
    Posts:
    1,070
    In the case of the example, transform.rotation is already a rotation -- it is just changing the notation to describe the rotation as an angle around an axis. I was responding to "I can't quite figure out what that function does from the description, or how to use it." -- so I said what it does and how it is used.

    To the main question:

    My problem answering this is that there isn't such a thing as an angle between two points. Two points are connected with a straight line, so there is no angle. If you want the the angle between that line and the x axis, you would do:
    Code (csharp):
    1.  
    2. var angle = Mathf.Atan2(B.y - A.y, B.x - A.x) * 180 / Mathf.PI;
    3.  
     
  12. Gaiyamato

    Gaiyamato

    Joined:
    Nov 19, 2009
    Posts:
    95
    ok.. so those x and y there would be the position of each vector? so ObjectA.transform.position.x and ObjectA.transform.position.y for example?

    So you could do:

    Code (csharp):
    1.  
    2. var ObjectA : Transform;
    3. var ObjectB : Transform;
    4.  
    5. function findAngle()
    6. {
    7.    var angle = Mathf.Atan2(ObjectB.position.y - ObjectA.position.y, ObjectB.position.x - ObjectA.position.x) * 180 / Mathf.PI;
    8.    return angle;
    9. }
    is that right?

    I still cannot see a clear explanation of how the Quaternion.ToAngleAxis works or when and why you would use it. :p
     
  13. Charles Hinshaw

    Charles Hinshaw

    Joined:
    Feb 6, 2008
    Posts:
    1,070
    That looks correct ( assuming that I'm remembering math class correctly ;) ) where angle is the angle of the line created between those two points, relative to horizontal (x-axis). Oh, I guess we could have done "* Mathf.Rad2Deg" instead of "* 180 / Mathf.PI", just to clear it up a bit.

    You could also do something like:

    Code (csharp):
    1.  
    2. var ObjectA : Transform;
    3. var ObjectB : Transform;
    4.  
    5. function findAngle()
    6. {
    7.    var rel = ObjectA.InverseTransformPoint(ObjectB.position);  
    8.    var angle = Mathf.Atan2(rel.y, rel.x) * Mathf.Rad2Deg;
    9.    return angle;
    10. }
    11.  
    12.  
    Which would be nice and concise since InverseTransformPoint puts ObjectB.position in terms of ObjectA's local space.


    There is already angle-axis rotation related stuff in the Quaternion class, for example Quaternion.AngleAxis(90, Vector3.forward), which is a 90 degree rotation around the z-axis. Quaternion.ToAngleAxis lets you take an existing rotation and get the angle and axis for that rotation.
     
  14. Gaiyamato

    Gaiyamato

    Joined:
    Nov 19, 2009
    Posts:
    95
    I think I am sort of understanding. lol.
    Learning Quaternion has been the biggest curve for Unity for me (aside from getting blasted networking to work properly AND efficiently).
    There seems to be 10 different ways to get a rotation or angle and all of them return completely different results. lol. Some of the doc entries are nothing more than "returns and angle" or "returns a rotation". Which is as useless as a slap in the face with a dead fish.

    Thanks for your help dude. :)
     
  15. roBurky_legacy

    roBurky_legacy

    Joined:
    Nov 7, 2009
    Posts:
    17
    Heh. I did have a bit of trouble trying to put what I was trying to do into words. I'm glad the message got through anyway.

    I think I've managed to get what I wanted working from the code placed in here, once I remembered I'm using the x and z axis, not the x and y axis.

    Thanks for your help, Charles.

    Out of interest, the maths behind this - what is it called? Is this the 'angles in other quadrants' business I've had difficulty understanding before?
     
  16. Charles Hinshaw

    Charles Hinshaw

    Joined:
    Feb 6, 2008
    Posts:
    1,070
    Yeah, I think so. I actually don't remember Atan2 being part of my trigonometry classes. (That doesn't mean it wasn't though) It is useful in a lot of cases involving vectors in Euclidean space because it takes into account the signs of both components unlike a regular arctangent function.

    Ugh... I just talked about math on a weekend. :cry:
     
unityunity