Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Unity Raycast Detect the point "Out" of collider, not only just "In"

By using Physics.Raycast in Unity, we can get hit information easily with the point, normal, collider name... But how can I get the "point" where ray go out of the collider?

Many thanks for your time thinking about this problem.

like image 424
user3792343 Avatar asked Dec 06 '25 23:12

user3792343


1 Answers

You could try casting a reverse ray only on the collider that has been hit so that you can determine where the ray enters from the back? The code below might work.

RaycastHit firstHit;
Ray firstRay = new Ray (origin, direction);
Physics.Raycast (firstRay, out firstHit, distance);

Vector3 reverseOrigin = firstRay.origin + (firstRay.direction * distance);
RaycastHit reverseHit;
Ray reverseRay = new Ray (reverseOrigin, (firstRay.direction * -1));
firstHit.collider.Raycast (reverseRay, out reverseHit, distance);
like image 177
ryanscottmurphy Avatar answered Dec 09 '25 18:12

ryanscottmurphy