Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Distance between Vector3 and Transform

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

  1. AvGaz

    AvGaz

    Joined:
    Dec 21, 2012
    Posts:
    82
    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:
    9,350
  3. AvGaz

    AvGaz

    Joined:
    Dec 21, 2012
    Posts:
    82
    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:
    82
    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:
    9,350
    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:
    82
    Genius, it worked! Thanks, saved a lot of time.