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.

Getting the direction of a vector?

Discussion in 'Scripting' started by Morgan, Jun 23, 2006.

Thread Status:
Not open for further replies.
  1. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    This sounds simple but I haven't found a way to do it.

    I know how to get just the magnitude of a Vector3. How can I read just the direction (x, y, z angles or a quaternion)?
     
  2. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Your best bet would probably be Quaternion.LookRotation

    However, there is no unique answer - if you have a vector, then you can put something that looks along this vector (that's what LookRotation does). However, that thing might freely roll around the vector, while still looking along it (that's why LookRotation also needs a "up" vector to align).
     
  3. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    [EDIT: Just saw Aras's post...]

    Perhaps a normalized vector is what you are looking for?
    directionVector = myVector.normalized;

    Leaves you with a vector with a magnitude of 1, so in other words, a direction vector.

    Hope that is what you were asking,
    -Jeremy
     
    TaylorVD and Nugget5 like this.
  4. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    OK, thanks. Z is irrelevant so I can just use world up.

    By the way, is Vector3.AngleBetween intentionally missing from the 1.5 manual? I tried using just Angle but it doesn't produce the same results as AngleBetween. So I'm still using AngleBetween and it still works fine in 1.5.
     
  5. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    AngleBetween returns radians. Angle returns degrees. Nobody wants to deal with radians, so AngleBetween is undocumented (but it's still there so that old code still works)
     
  6. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    I see. Sometimes you need radians though!

    Thanks for clearing that up.
     
  7. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Multiply your degrees by Mathf.Deg2Rad;

    -Jeremy
     
  8. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Yes, I use Mathf to go back and forth at will.
     
  9. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,266
    So how do you get the direction from one vector to another?

    (myVector1 - myVector2).normalized ? I never did vector math so I have to get everything from the forums and examples ;).
     
  10. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Yes. Tough if you want it from vector1 to vector2, you do
    Code (csharp):
    1. (vector2-vector1).normalized
    That is, end point minus start point, then normalize.
     
    vegn and Ash-Blue like this.
Thread Status:
Not open for further replies.