- Home /
How does a raycast really work?
Does anybody know how to implement a custom version of Unity's raycast or UDK's Trace?
Might be an overwhelming and a very complicated question, but how would I check if my ray or 3D vector would return an intersection with something like a polygon?
Here is the source I've found http://www.permadi.com/tutorial/raycast/rayc1.html
Thank you!
Answer by robertbu · Jan 08, 2014 at 12:38 AM
It depends on what you are trying to do. I assume your polygon is on some plane. You can use Unity's mathematical Plane class to find the point on the plane. They you can test to see if the point is inside or outside the polygon. Google "point inside outside polygon" and you will find many hits (some with source you can translate).
As an alternate, you could just build the polygon into a mesh and allow Unity's Physics.Raycast() or Collider.Raycast() to do the detecting. Here is a Wiki script that might be helpful if you go down that road.
http://wiki.unity3d.com/index.php/Triangulator
What approach you take (and there are others plus refinements of these two) is highly dependent on the nature of your app.
Answer by aldonaletto · Jan 08, 2014 at 01:54 AM
Physics.Raycast or UDK's Trace are related to spatial search, differently of the text you've linked (which's actually about rendering, and is pretty outdated). Physics.Raycast casts an imaginary ray from the origin in the desired direction and checks whether it hits any collider. In many physics engines (and probably in Unity and UDK) it's done as a search in an internal database where the engine keeps info of all colliders in scene, arranged so that spatial searches may be done quickly. The ray is first tested against the bounding box of each collider; when it's found that a bounding box is hit by the ray, the test is done against the actual collider shape. Spherical, capsule and box colliders are tested with relatively simple operations, but mesh colliders must be tested triangle by triangle.
Implementing a custom 3D raycast test is for sure a pain in the ass - maybe this paper helps you to find the bounding box intersection. A 2D raycast is fairly less complicated, but still a hard job - google for line-polygon intersection algorithms.
Your answer
Follow this Question
Related Questions
detection raycast too late 2 Answers
Checking detection of gameobject correction 2 Answers
Decals on corners 1 Answer
raycast hit not detecting && Nav mesh agent ai stealth 1 Answer
I don't know why i can't detect by ray sth. tagged, 1 Answer