Search Unity

Distance between Vector3 and Transform

Discussion in 'Scripting' started by AvGaz, Sep 14, 2017.

  1. AvGaz

    AvGaz

    Joined:
    Dec 21, 2012
    Posts:
    74
    I'm using leap to move an object towards a vector3.

    However, I want something to happen in script when it arrives, so I'm intending to use distance to know when the object is near the intended point, and change a value on arrival.

    Thing is, the game object is using lerp towards a vector3, but the actual game object being moved is a transform.position.

    How do you compare them to get distance?

    Thank-you.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    7,117
  3. AvGaz

    AvGaz

    Joined:
    Dec 21, 2012
    Posts:
    74
    Thanks, how would you compare though, you'd have to assign the transform.position to 'other' so it knows where it's comparing too... you can't assign a vector3 directly...
     
  4. AvGaz

    AvGaz

    Joined:
    Dec 21, 2012
    Posts:
    74
    the target position is in script only, as a vector3, there's no transform.position to assign
     
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    7,117
    if want to compare this transform position, to vector3 target,
    Code (CSharp):
    1. float dist = Vector3.Distance(transform.position,target);
    it takes 2 variables, both vector3
    Distance(Vector3 a, Vector3 b);
     
  6. AvGaz

    AvGaz

    Joined:
    Dec 21, 2012
    Posts:
    74
    Genius, it worked! Thanks, saved a lot of time.
     
unityunity