- Home /
How can I find the distance from one object to another using raycasts?
How can I calculate the distance from one object to the surface(NOT center) of another using raycasts, similar to LIDAR (LIght Detection And Ranging, which is basically RADAR with lasers). I want to be able to update the distance with FixedUpdate(). basically, it is an altimeter that i want to use with spherical planets. The object i want to detect my distance from might be blocked by another object, but i cant have any objects on the Ignore-Raycasts list.
Use Collider.Raycast(). This allows you to raycast against a single specific collider rather than raycast through the scene.
that helps a lot, but i still don't know how to find the distance
Answer by aldonaletto · Dec 17, 2013 at 04:15 AM
In order to find the distance to the surface of some object without being disturbed by other objects in between, use Collider.Raycast like @robertbu suggested, and measure the distance from your object to the hit point. Supposing that the script is attached to your object and target is the other object's transform, the code could be like this:
var target: Transform; // planet transform
private var distance: float;
function FixedUpdate(){
// get current position:
var myPos: Vector3 = rigidbody.position;
// create ray from this object to the planet center:
var ray: Ray = new Ray(myPos, target.position - myPos);
var hit: RaycastHit;
// do a Raycast that sees only the target collider:
if (target.collider.Raycast(ray, hit, Mathf.Infinity)){
// save distance to the surface in variable distance:
distance = Vector3.Distance(myPos, hit.point);
}
}
function OnGUI(){
// show the distance with one decimal digit:
GUI.Label(Rect(10,10,120,30), distance.ToString("F1"));
}
EDITED: The field hit.distance already contains the distance from the hit point, thus you don't actually need to calculate it:
...
if (target.collider.Raycast(ray, hit, Mathf.Infinity)){
// save distance to the surface in variable distance:
distance = hit.distance;
}
}
This is exactly the answer we put above, save for the fact that we didn't code it for him.
If I understand OP correctly, I believe you need to perform 2 raycasts to get the true distance from a mesh to a mesh.
One raycast from A to B, using B's collider. Another from B to A, using A's collider. Then do a Vector3.distance( hitpointA, hitpointB ) to get the true distance.
That's right for finding the distance between two surfaces: do a raycast from the object to the planet, then do a second raycast from the hit point to the object and get its hit.distance. But an actual altimeter measures the distance between itself and a ground reference, thus I suppose that a single raycast will do the job.
Your answer
Follow this Question
Related Questions
Raycast origin overlapping surface of collider counts as a hit 0 Answers
Raycast exit point of collider 0 Answers
Raycasting doesn't hit terrains 0 Answers
What's wrong with my RayCastHit2D? 0 Answers
Another Raycast Issue. 0 Answers