Search Unity

Distance script check

Discussion in 'Scripting' started by gore23, Jun 3, 2011.

  1. gore23

    gore23

    Joined:
    Jan 18, 2011
    Posts:
    324
    How would I get the distance between two objects... I did this but I cant figure out what im doing wrong. ...




    Code (csharp):
    1.  
    2. var objectA : GameObject;
    3.  
    4. var objectB : GameObject;
    5.  
    6. private var moved : int = 0;
    7.  
    8. function Update () {
    9.    
    10.     var dist = Vector3.Distance(objectA.Vector3, objectB.Vector3);
    11.     print ("Distance to other: " + dist);
    12.    
    13. }
     
  2. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    You should be using ObjectA.transform.position and ObjectB.transform.position as those have the values of the objects' positions.
     
  3. gore23

    gore23

    Joined:
    Jan 18, 2011
    Posts:
    324
    place them in the round brackets ? instead of the vector3 ?
     
  4. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    Yes, put them there. You aren't given them a Vector3 now, you have to give it an actual Vector3 with values, such as a transform's position (which is a Vector3).
     
  5. gore23

    gore23

    Joined:
    Jan 18, 2011
    Posts:
    324
    by chance would you have a skype account? and if do by chance could we be friends and if i have some quick script problem get your help ? and maybe be buddies test projects or something also ? be alot faster than forum XD

    Thank you the script works now btw =)
     
  6. JRavey

    JRavey

    Joined:
    May 12, 2009
    Posts:
    2,377
    I do have a Skype account, but I'm actually a pretty busy person. So if you have questions, post them here with as much detail as possible and one of the many competent programmers will surely help you.