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

Calculating distance between two objects never returns 0 when they're colliding?

Discussion in 'Scripting' started by Zebbi, Aug 4, 2019.

  1. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    I'm trying to get the distance between two objects
    Code (CSharp):
    1. Vector3.Distance(target.transform.position, transform.position);
    but when the two objects are touching, the distance returns 0.58 in my case, when I expected 0. Is this because it because it calculates from the centre of each gameobject, and the distance from the centre to the edges is added on? I tried using closestPoint and closestPointOnBounds, but it didn't make any difference. How do I calculate the distance of two items from each other from the closest part of both of them?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,522
    The code above just tells the distance between their Transform positions.

    If the objects have convex-ish colliders, I suppose you could do something like this to get an approximation, but there could be closer parts than this:

    - turn off object 1
    - raycast at object 2, see where you hit it, save that point (it is in the RaycastHit struct)
    - turn on object 1
    - turn off object 2
    - raycast at object 1, see where you hit it
    - turn on object 2
    - take the distance between point 1 and point 2.

    But for instance if the objects were two barbells, this won't be helpful, as the weight lobes could get closer than the handles.
     
  3. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    Thanks, that's a great idea, I'm doing a distance check for all of the enemies as a way to reduce the width of the cone of my vertical LOS, which is just a vector3 Dot forwards, I'm not sure how expensive this would be for doing this onloop for several dozens of enemies, but it's definitely an option. Is it possible to subtract the size of the collider from the centre of both transform positions as another solution? Although I'm not sure how collider sizes translate to distance units.
     
  4. doctorpangloss

    doctorpangloss

    Joined:
    Feb 20, 2013
    Posts:
    270
    Efficiently finding the closest point on a polygonal mesh is pretty complicated. For example, two geometries that look like this (two horseshoes with their ends almost touching), next to each other:
    ⊂⊃
    There are actually two pairs of closest points, which would give you different answers for your "cone of vertical LOS," and since there's no geometry between the bounding-box-centers, that couldn't be helpful either.

    My recommendation is to just use bounding spheres whose radius you set manually so that it looks how you need it to.
     
  5. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    I'm just using character controller capsules on both player and enemies, so it doesn't have any complex mesh to work out, that would have a fixed size that could be calculated reliably, wouldn't it?
     
  6. Zebbi

    Zebbi

    Joined:
    Jan 17, 2017
    Posts:
    521
    Is there a correct way of calling closest point? I'll probably just use the raycast method, but if using primitives, couldn't the size of both bounds /2 be subtracted from the distance?