- Home /
Question by
JoelAtMoodkie · May 04, 2010 at 01:59 PM ·
raycastrayhit
Calculate how far a ray has intercepted an object.
Lets say I've cast the following ray (in C#) to act as a feeler in-front of an object to detect wall collisions:
Physics.Raycast (transform.position, transform.forward*10, out hit, 10, mask);
From the 'hit' information, what would be the quickest way to work out how far the ray has intercepted the object?
All the best, Joel.
Comment
Answer by jester · May 04, 2010 at 03:54 PM
something like:
vector3 distanceVector = hit.point - transform.position;
foat distanceToHit = distanceVector.magnitude;
that gets you the linear distance to the object that was hit.
Your answer
![](https://koobas.hobune.stream/wayback/20220612211119im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Physics.Raycast (ray, hit) strange behaviour 3 Answers
Need Help with RayCast. No Vector? 1 Answer
[Unsolved] Raycast isn't casting 0 Answers
Change raycasting 1 Answer
How to use raycast on generated objects. 0 Answers