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 two objects?

Discussion in 'Scripting' started by stuthemoo, Dec 30, 2009.

  1. stuthemoo

    stuthemoo

    Joined:
    Sep 17, 2005
    Posts:
    182
    Hello,

    I need to write a script that tells the player how far away object 1 is from object 2.

    But I'm having difficulty. Could someone help point me in the right direction please :)

    Thanks a lot,
    Stuart
     
    darshan-kirubakaran likes this.
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    In C#
    Code (csharp):
    1. float distance = Vector3.Distance (object1.transform.position, object2.transform.position);
     
  3. Tinus

    Tinus

    Joined:
    Apr 6, 2009
    Posts:
    437
    Yep, here's how that works internally (probably): You find the difference between the two vectors (v1, v2) by subtracting them from one one another and storing the result in a third vector:

    Code (csharp):
    1.  
    2. Vector3 difference = new Vector3(
    3.   v1.x - v2.x,
    4.   v1.y - v2.y,
    5.   v1.z - v2.z);
    6.  
    The distance between the two vectors is this vector's length (or 'magnitude', a property which Unity handily provides for you), which you could manually calculate by using some trigonometry:

    Code (csharp):
    1.  
    2. float distance = Math.Sqrt(
    3.   Math.Pow(difference.x, 2f) +
    4.   Math.Pow(difference.y, 2f) +
    5.   Math.Pow(difference.z, 2f));
    6.  
     
  4. stuthemoo

    stuthemoo

    Joined:
    Sep 17, 2005
    Posts:
    182
    Thanks a lot for your help. I get the basic idea.
    But I should of said earlier that I'd only really understand javascript :)
     
    harlandpj likes this.
  5. stuthemoo

    stuthemoo

    Joined:
    Sep 17, 2005
    Posts:
    182
    Hmm...

    I still can't get it to work. :(

    Could someone write a script for me in Javascript?
    I also want the value to be displayed as a Gui.

    Thanks,
    Stuart
     
  6. Wox

    Wox

    Joined:
    Dec 14, 2009
    Posts:
    93
    Try this, just a small test but maybe it will help.
     

    Attached Files:

  7. stuthemoo

    stuthemoo

    Joined:
    Sep 17, 2005
    Posts:
    182
    Thanks! That worked perfectly. I just found this in the script reference with a perfect example as well:

    http://unity3d.com/support/documentation/ScriptReference/Vector3.Distance.html

    I probably should've looked harder :oops:

    Now I just need to display the speed my object is traveling :) which so far seems to be much harder :roll: and I haven't even started to think about converting it to Km/h
     
  8. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I am not very comfortable with JavaScript, but I hope you understand the idea.
    Code (csharp):
    1. private var previousPosition: Vector3;
    2. private var speed: float;
    3.  
    4. function Speed () {
    5.     return (speed);
    6. }
    7.  
    8. function SpeedInKmH () {
    9.     return (speed * 3.6);
    10. }
    11.  
    12. function Start () {
    13.     previsousPosition = transform.position;
    14. }
    15.  
    16. function Update () {
    17.     var distance: float;
    18.     distance = Vector3.Distance (previousPosition, transform.position);
    19.     speed = distance / Time.deltaTime;
    20.     previousPosition = transform.position;
    21. }
     
    SmithySFC likes this.
  9. gooncorp

    gooncorp

    Joined:
    Dec 30, 2011
    Posts:
    131
    give me a break. why not just give a simple answer dude. the most complex coders in the world will always use the EASIEST code.

    why isnt the code more versatile, for example:

    why cant i just name 2 objects
    lets say i have 2 objects named object1 and object2
    then just use:

    if (Vector3.Distance(object1,object2) < 100) {}
    im finding i have to write complex scripts just to do a simple thing like find a distance between 2 objects?

    the other game engines i have worked with make this much easier.
     
    Last edited: Jan 6, 2012
    Tcrakman and isotronic like this.
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's always a good idea to pay attention to dates when posting on forums. Trying to start an argument over a 2-year-old post isn't the best use of your time.

    You can. That is in fact what you do, though you'd need to specify ".position". Vector3.Distance(object1.position, object2.position)

    You don't have to write complex scripts, and I seriously doubt you can name any game engines where it's any easier, considering that it's a trivial 1-line command in Unity. Probably best to make sure you actually understand what it is you're doing before making unfounded complaints.

    --Eric
     
  11. gooncorp

    gooncorp

    Joined:
    Dec 30, 2011
    Posts:
    131
    thanks guys. sorry for my attitude i have figured out the paradigm. here is a video from the game i am currently designing. im having to generate artificial gravity for certain scenarios so you can see things start to get complex.

    figuring out new things makes me angry and irrate, but i just keep trudging.

    here is a video:

    http://www.youtube.com/watch?v=caFDvgnPXNs&hd=1

    im am currently learning the unity engine from the floor up. you will be seeing more of me on these threads soon.

    Gooncorp
     
    mpeschel10 likes this.
  12. Lewenos

    Lewenos

    Joined:
    Apr 20, 2011
    Posts:
    4
    var speed : float;
    var speedGUI : GUIText;

    function Update(){
    speed = rigidbody.velocity.magnitude;
    speedGUI.text = speed.ToString();
    }

    if i remember right....
     
  13. Thomas12345

    Thomas12345

    Joined:
    Jan 15, 2013
    Posts:
    1
    I wrote a simple editor script that does this. For those whom still end up in this tread. (Like myself ;P)

    NOTE: I created it for unity 5.6
     

    Attached Files:

  14. faridistiqlal

    faridistiqlal

    Joined:
    Jan 29, 2018
    Posts:
    2
    hello. can we make distance for three object? thanks
     
  15. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Do you mean like if you wanted to add them together or something? Explain what you mean :)
     
  16. Honorsoft

    Honorsoft

    Joined:
    Oct 31, 2016
    Posts:
    81
    I haven't seen much that can't be done in programming, it just can get complicated. I also don't know why you would need that, the function gives the distance between 2 points in space, a third point would mean extra distances between point A and B, A and C, and B and C. It's a two-point function, it's like asking 'if you can add a third vector to a 2D vector?', and the answer's no, it's a 2D vector with only 2 vectors(and Distance is a '2D' function, Pos1 and Pos2). You could make whatever custom function you wanted to compare 3 points in space so have fun.
     
  17. rooster2

    rooster2

    Joined:
    Mar 23, 2015
    Posts:
    25
    i get this page might be dead but how do i get the distance from one object to multiple so for example say i have the main character and to attack the closes one it needs to check the distance from 4 enemies(min at the moment) and select the closest enemy to it how would i do that as using vector3.distance gives me a overload error
     
  18. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You have to check all 4 and find the lowest value. :)
     
  19. rooster2

    rooster2

    Joined:
    Mar 23, 2015
    Posts:
    25
    Cheers