- Home /
How are raycasts actually implemented?
How is Physics.Raycast implemented in Unity? What is under the hood or how would you do it on your own?
Answer by BastianUrbach · Sep 18, 2018 at 05:27 PM
Find all objects where the ray intersects the bounding box of the collider (Bounds.IntersectRay). For each triangle in those colliders (assuming you're using MeshColliders), find the point where the ray intersects the plane of the triangle (Plane.Raycast). Then test if that point is the triangle. For a triangle ABC and a point P that is the case if and only if Cross(AB, AP), Cross(BC, BP) and Cross(CA, CP) all point in the same direction.
Under the hood, Unity uses Nvidia PhysX for physics but that probably won't help you much and unfortunately I don't know how PhysX works in detail.
"Then test if that point is IN the triangle" is what you probably wanted to say. So basically I must check every collider present in the scene in the case I have infinite ray. Thanks for the answer, it's what I wanted to hear.
Your answer
![](https://koobas.hobune.stream/wayback/20220612174754im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem With RayCast 3 Answers
thousands of raycasts in nested for loop 0 Answers
Need help with Raycasting 1 Answer
Passing values from Shader to C# script 0 Answers
Raycast problems in my AI 1 Answer