Hello,
How do I get the distance between two GameObjects in Untiy 3D ?
Hello,
How do I get the distance between two GameObjects in Untiy 3D ?
http://unity3d.com/support/documentation/ScriptReference/Vector3.Distance.html
Thanks for searching before you posted. /sarcasm
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.
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 ?
Vector3.Distance: checking distance across 3 axis.
Vector2.Distance: checking distance across 2 axis.
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.)
Thanks.
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
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
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
var dist = Vector3.Distance(objectA.transform.position, objectB.transform.position);
just get the extents of their bounds and subtract that from the distance
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
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
Haha, its really interesting to read something I wrote so long ago. I sure have changed
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 ?
PLEASE EXPLAIN AND NO CRAP ANSWERS PLEASE
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.
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.
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.
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))
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
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…
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:
http://forum.unity3d.com/threads/118345-Sending-data-from-one-scene-to-the-next
I would be most thankful!
Are you just trolling for youtube hits? You’ve posted the same ridiculous rant in 2 ancient threads and then apologized in the same manner in both. Not the best way to make a first impression my friend. I, personally, won’t be watching any of your videos.