- Home /
Is it possible to measure distance's between raycast intersections?
Is it possible to measure the distance between point a and point b. The redline is a raycast. hit.point can be used to find point a but what about point b?
Answer by YoungDeveloper · Aug 27, 2013 at 09:45 PM
Hi, raycast can hit only forward faces. You could cast another ray backwards, or you could create another collider and place it from side a, cast two rays, one will get the hit_b.point, other hit_a.point (ignoring collision with the first collider b), then you can measure distance between these two points.
If you cast a ray backwards and there are 2 or more GameObjects between startPoint and the maximum distance. (So the ray goes through 2 or more GameObjects) Then you will get a wrong value for hit_b.point, becous the backwards ray will hit the second (or more) GameObject first.
Is there a solution for this problem without creating another collider?
Yes there is. Just don't use Physics.Raycast for the second reverse ray but Collider.Raycast. Collider.Raycast can only hit that collider, nothing else.
Thanks! :)
I also found an option with "Physics.RaycastAll". If you use it in normal- and reverse direction, you will get 2 arrays with all the intersections of the ray.
Now it's possible to calculate the distance (tickness) from every GameObject the ray intersects. => First element from arrayA relates to the last element from arrayB, ...
RaycastHit[] hitInfoA;
RaycastHit[] hitInfoB;
hitInfoA = Physics.RaycastAll(startpoint, direction, maxLengte, layer$$anonymous$$ask);
hitInfoB = Physics.RaycastAll(endpoint, -direction, maxLengte, layer$$anonymous$$ask);
Answer by toddisarockstar · Mar 15, 2016 at 01:32 AM
Did we forget about raycastall?
var dist:float;
var hits: RaycastHit[];
hits = Physics.RaycastAll(transform.position, transform.forward, 100.0F);
if(hits.Length>1) {dist = Vector3.Distance(hits[1].point, hits[0].point);}