# How do I get the distance between two GameObjects ?

Discussion in 'Scripting' started by Jeffmaliboe, Feb 9, 2011.

Not open for further replies.
1. ### Jeffmaliboe

Joined:
Jan 1, 2010
Posts:
85
Hello,

How do I get the distance between two GameObjects in Untiy 3D ?

Joined:
Dec 31, 2010
Posts:
3,137
3. ### Cameron_SM

Joined:
Jun 1, 2009
Posts:
915
If you don't need the exact distance and just want to know if one vector is closer than another than using:

(a - b).sqrMagnitude

Is a lot faster.

4. ### Jeffmaliboe

Joined:
Jan 1, 2010
Posts:
85
Then what about the Vector2.Distance ?
If Vector3.Distance is used to get the position of GameObjects in the scene,then how is the Vector2.Distance used then ?
Is Vector2.Distance also for 3D GameObjects in the scene or something else ?

5. ### DanielQuick

Joined:
Dec 31, 2010
Posts:
3,137
Vector3.Distance: checking distance across 3 axis.
Vector2.Distance: checking distance across 2 axis.

6. ### flaminghairball

Joined:
Jun 12, 2008
Posts:
868
Vector3.Distance can be used if you want to determine the distance between two gameobjects.
Vector2.Distance can be used if you're making a 2D game, or for GUI elements(for example, determining how far the mouse traveled over a series of frames.)

Joined:
Jan 1, 2010
Posts:
85
Thanks.

8. ### EvanDelaGrammaticas

Joined:
Aug 29, 2011
Posts:
5
Thanks for the link, even though the origional poster may not have searched, I did - and I got this answer. So please don't have a go at someone for asking a question, and really if you don't like it - just don't bother answering - in this case though - thanks

9. ### bigmisterb

Joined:
Nov 6, 2010
Posts:
4,221
I can give a more contorted answer.... Do a physics.Raycast (RaycastAll) from each object to the other, and see the hit that is on that object, then subtract the remainder of the distance. The middle part is the physical distance between colliders.

If distance wise, they are 50, but the colliders show 45 and 43, the physical distance is 50 - (50 - 45) - (50 - 43). (thats 42 btw)

Thus contorting the OP question even further. lol

EZaca likes this.
10. ### Wulf

Joined:
Aug 31, 2011
Posts:
7
that's a fine point to bring up, Big!

If we were trying to find the distance between two ENORMOUS spaceships in a game perhaps, the vector3.distance might not be useful.
find the distance between their surfaces instead. Smart

11. ### samloeschen

Joined:
Mar 22, 2010
Posts:
268
var dist = Vector3.Distance(objectA.transform.position, objectB.transform.position);

just get the extents of their bounds and subtract that from the distance

Last edited: Sep 2, 2011
12. ### bigmisterb

Joined:
Nov 6, 2010
Posts:
4,221
Ah, but that wont be exact enough, since the bounds are nothing but big boxes. . We HAVE to be really, REALLY anal about everything.... lol

13. ### samloeschen

Joined:
Mar 22, 2010
Posts:
268
In that case, possibly the best solution might be to iterate through all the vertices in objectb that are hit by a sphere cast coming from objecta, and average their distances? :3

14. ### DanielQuick

Joined:
Dec 31, 2010
Posts:
3,137
Haha, its really interesting to read something I wrote so long ago. I sure have changed

Callindrill and EZaca like this.
15. ### gooncorp

Joined:
Dec 30, 2011
Posts:
131
doesnt sound like you guys know what your talking about. he is asking a simple question. why dont you explain it a bit better. thanks for pointing out the obvious like what the commands mean LOL.

dont you need to add tags to things? why all the jargan with a simple command in script like object distance.

explain it a bit better. first you have to name these objects do you not? and dont just cite some stupid paragraph off their scripting reference. i want real code that works.

how come i cant do this?

if (Vector3.Distance (object1.transform.position, object2.transform.position) > 1) {
print ("worked");
}

and if i can how the hell do i name the objects object1 and object2 ?

if i wanted to do some huge procedural script to find a relative distance i may as well be coding in C
and not a game engine for crying out loud. sorry to be a bitch but this is slowing me down.

Last edited: Dec 31, 2011
Deleted User, Neodymus and ppoiuytre1 like this.
16. ### npsf3000

Joined:
Sep 19, 2010
Posts:
3,830
If you don't want to use the scripting reference then you do not want to be a programmer.

Consider moving your post to the commercial section and offer an appropriate remuneration, or collab if the remuneration is non-financial.

Thank you for your courteous behavior and obvious effort expended. It's good to know our time is valued.

/RANT

Edit: A reliable source has informed me that Gooncorp may be a reference to a group of trolls. Given the demonstrated behavior that's not a surprise. I suggest ignoring, or if you really must, reporting.

Last edited: Dec 31, 2011
rahulk1991 likes this.
17. ### Dabeh

Joined:
Oct 26, 2011
Posts:
1,614
I hope your name doesn't come from goons from somethingawful. Because if it is, then that would explain your attitude and your soon to be expected ban.

18. ### wccrawford

Joined:
Sep 30, 2011
Posts:
2,040
Wouldn't that be 38?

Logically, the distance from the center of A to the nearest point (on the ray to B) of A is 5. (50-45) From the center of B to the nearest edge of B is 7. (50-43) So the distance from the nearest edge of A to the nearest point of B is 38. (50 - (5 + 7))

19. ### cybervinus

Joined:
May 14, 2009
Posts:
12
Not to be a dink, but there's a few corrections to be made with regards to some of the distance finding algorithms suggested in this thread

1) Finding the distance between the two large spaceships WOULD in fact be best accomplished by using the bounds of their bounding boxes - two large spaceships would have their collision meshes made up of several primitives, not just a single box, thus eliminating the issue of accuracy. Which leads on to...

2) Finding the distance between two large spaceships by iterating through each vertex would not only be wrong because the closest point might occur on an edge, but also redundant because the ship would have a far more simple collision mesh that you would use instead.

Just sayin'

Oh and while I'm here, if some of you could find the time to peruse my thread about passing data between scenes: