Vector3.Distance edge of model

The problem is simple. I have a 3D model and a cube in my game world. On right mouse click I tell my model to run towards the cube until the Vector3.Distance between the two is less than 1. The problem with this is, it seems the “Distance” is from the center of my model to the center of the cube. So when practicing this with a cube with a large width, my model will continue to run at the cube even though he is already colliding with the edge.

How do I check the distance between the edge of my model and the edge of the cube? My intention is to replace the cube with an enemy at some point, but enemies will be different sizes.

Thank you.

Raycasting is far heavier than needed.

If checking distance from the AABB is enough, here is a quicker alternative.

You can calculate the distance between the 2 colliders enclosing the objects using an integrated function.

Here’s a general function to find the point of a collider that is closest to a point

The trick is:

  1. we start from the positions of the 2
    objects
  2. we get the ClosestPointOnBounds of
    Obj1 collider from Obj2 position
  3. we get the ClosestPointOnBounds of
    Obj2 collider from Obj1 position
  4. we measure the distance between the
    2 ClosestPointOnBounds we got

Just make sure both objects have a Collider component (sphere, box, whatever)

using UnityEngine;
using System.Collections;

public class TrueDistance : MonoBehaviour {
    public GameObject otherObject;
   
    Vector3 closestSurfacePoint1;
    Vector3 closestSurfacePoint2;
   
    // Use this for initialization
    void Start () {
        // the surface point of this collider that is closer to the position of the other collider
        closestSurfacePoint1 = collider.ClosestPointOnBounds(otherObject.transform.position);
       
        // the surface point of the other collider that is closer to the position of this collider
        closestSurfacePoint2 = otherObject.collider.ClosestPointOnBounds(transform.position);
       
        // the distance between the surfaces of the 2 colliders
        float surfaceDistance = Vector3.Distance(closestSurfacePoint1, closestSurfacePoint2);
        Debug.Log(surfaceDistance);
    }
   
    // Update is called once per frame
    void Update () {
        // visualize the different distances (debug)
        Debug.DrawLine(transform.position, otherObject.transform.position, Color.yellow);
        Debug.DrawLine(closestSurfacePoint1, closestSurfacePoint2, Color.magenta);
    }
}

I was hoping someone would give a nice ‘pro’ answer, but this is how I tackled the problem.

  • calculate distance between the center of 2 objects
  • raycast from each object
  • each raycast should hit collider of other object
  • raycast distance should be less than center distance, the difference being the distance between the object’s center and the edge of the collider facing the other object
  • calculate both colliders’ difference , and subtract them from the center distance
  • value should be distance between the colliders.

Create a new scene, attach the below script to the camera or an empty gameObject.
Create 2 cubes, and drag-n-drop them in the script’s Inspector ( obj1 and obj2 ).
Change the scene view to ‘wireframe’ so the debug lines can be seen.
Hit Play and move the cubes around. The GUI shows the difference between the center’s and colliders.

#pragma strict

public var obj1 : Transform;
public var obj2 : Transform;

private var centerDiff : float = 0.0; // difference between the centers
private var colliderDiff : float = 0.0; // difference between the collider edges

private var rayDist1 : float = 0.0;
private var rayDist2 : float = 0.0;

private var drawlineOffset : Vector3 = Vector3(0, 0.02, 0); // for the drawlines to not render over each other


function Update() 
{
	centerDiff = (obj2.position - obj1.position).magnitude; // distance between 2 objects
	
	var rayHit : RaycastHit;
	
	if (Physics.Raycast( obj1.position, Vector3.zero - (obj1.position - obj2.position).normalized, rayHit) ) 
	{
		Debug.DrawLine(obj1.position, rayHit.point, Color.red);
		rayDist1 = Vector3.Distance(rayHit.point, obj1.position);
	}
	
	if (Physics.Raycast( obj2.position, Vector3.zero - (obj2.position - obj1.position).normalized, rayHit) ) 
	{
		Debug.DrawLine(obj2.position + drawlineOffset, rayHit.point + drawlineOffset, Color.green); // offset so line can be seen
		rayDist2 = Vector3.Distance(rayHit.point, obj2.position);
	}
	
	colliderDiff = centerDiff - ( centerDiff - rayDist1 ) - ( centerDiff - rayDist2 ); // colliderDiff - (collider2 radius) - (collider1 radius)
}

function OnGUI() 
{
	GUI.Box(Rect(10, 5, 200, 25), "center Difference = " + centerDiff.ToString() );
	GUI.Box(Rect(10, 35, 200, 25), "collider Difference = " + colliderDiff.ToString() );
}

(this is the same answer I gave for http://answers.unity3d.com/questions/273365/upper-edge-of-an-object.html , was hoping there was a more ‘pro’ method someone could provide, but this works while the colliders don’t overlap)

[1389-collider+diff.png|1389]

Maybe Raycast can do the job. Not used it yet, just saw it in a script.

http://unity3d.com/support/documentation/ScriptReference/Physics.Raycast.html