- Home /
How to find the closest distance between two objects?
One of the objects is always a rotated straight line (a square, really). The other is not constant, but is a character shape. The ColliderDistance2D would work, if it were for 3D. (However, after a test I can verify that the distance is slightly wrong anyway). A raycast or a rb.SweepTest is too expensive, specially since the distance between both shapes can be very large, and also because the whole point of all of this is a broad phase for collision detection.
Sorry but this question is too vague. You said you need it in 3d. A line does exist in 3d just as in 2d. A "square" can also exist in 3d however it needs an additional orientation. Also do you really meant square or a rectangular shape? The most important question is however what is the "other" shape? Do you actually talk about the surface of colliders? Or do you mean arbitrary meshes? If you talk about actual colliders, why do you think a sweeptest is too expensive? Unity uses PhysX for 3d physics. It probably doesn't matter what collision detection system you come up with it won't be any faster anyways unless you have some real simplifications. This brings us back to my question again: What is that other shape? What do you mean by "character shape"?
This question need some clarifications. Please edit your question and include more details. $$anonymous$$aybe a screenshot / drawing would help. If your "character shape" is actually a capsule it's kinda trivial.
It is a 3D rectangular shape, but the collision will always happen below so it can be treated like a line. And is not because PhysX is too slow or anything: am trying to create new physics because that engine doesn´t work for me. I think that sweeptests can be slow because I know they are made with a bunch of raycasts and other collision shapes (the documentation isn´t much more clear); but I know that raycasts are more expensive the longer they are, so they shouldn´t be very good (besides, the whole point of this check is to reduce the amount of sweeptests per frame, otherwise I would be doing one for every object in scene every frame). And the character shape is really just that: a character. I don´t know what needs to be explained there. However, I don´t have the model yet, because am still deciding on how should it look (and am probably not gonna do it until the end of the development). And is not even constant: this distance check also needs to be done with other humanoid forms. So right now, am using capsules to test everything. And I don´t know how to do that "trivial" stuff with capsules. It doesn´t seem that obvious.
Answer by Magso · Dec 02, 2019 at 10:38 PM
I found this buried in Unity answers and used it ever since.
GameObject nearest;
void Start()
{
var distance = Mathf.Infinity;
var position = transform.position;
var objects = GameObject.FindGameObjectsWithTag("Tag");
foreach(GameObject thisObject in objects)
{
var diff = (thisObject.transform.position - position);
var curDistance = diff.sqrMagnitude;
if(curDistance < distance)
{
nearest = thisObject.gameObject;
distance = curDistance;
}
}
}
It basically starts distance
from infinity and checks if the distance of the next object in the array is nearer than distance
, if so distance
is set to that objects distance and in turn finds the nearest.
Thanks for the reply, but this only gives the difference between positions, right? Am looking for the distance between the closest vertices of two specific figures. Your code shows which object from a bunch is the closest to a single object, and gets the distance from the positions (presumably the centers), which is never the closest unless you are comparing points... Is there a way of seeing which is the last vertex in a specific direction and then compare positions to get distance?
Your answer
![](https://koobas.hobune.stream/wayback/20220612215613im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to draw a line from the player to the nearest object? 0 Answers
Get point on surface of Collider2D from inside collider. 1 Answer
inconsistent results calculating the distance moved by an object 1 Answer
Find closest "item" object by NavMesh navigation distance performantly 0 Answers
How do i get my script to work? 2 Answers