- Home /
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.
Well it would be easier with a radius - you would just take that off the sizes of both the mesh and the cube.
Answer by AlucardJay · Jun 25, 2012 at 05:25 AM
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)
Thanks so much for the response. Sorry it took so long to respond. I had trouble logging in. I'll give this a shot, it seems like a reasonable solution.
Answer by Agostino · Nov 13, 2013 at 03:55 PM
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 http://docs.unity3d.com/Documentation/ScriptReference/Collider.ClosestPointOnBounds.html
The trick is:
we start from the positions of the 2 objects
we get the ClosestPointOnBounds of Obj1 collider from Obj2 position
we get the ClosestPointOnBounds of Obj2 collider from Obj1 position
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);
}
}
Answer by Huma · Jun 20, 2012 at 08:26 PM
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
how it will work in RayCast and if i did that with Ray Cast In a direction it wont work only if i made all the cubes in one same direction From The Character
Your answer
![](https://koobas.hobune.stream/wayback/20220613073044im_/https://answers.unity.com/themes/thub/images/avi.jpg)