Wikipedia: Raycasting
Raycasting is the use of intersection tests to solve problems in ROBLOX. The most common use of raycasting is to determine the first object intersected by a ray. This is done by casting a virtual ray from a certain point in a direction and determining the first surface it intersected with.
In the middle of 2011, ROBLOX implemented the FindPartOnRay
method, which finds the first part a ray, represented by a Ray
object, collides with when cast. It was already possible, using user-created algorithms, to implement this behavior, but such algorithms are usually very expensive in performance. Algorithms implemented at a lower level (in this case, in C++) are usually faster and more performant than algorithms at a higher level. Even with the FindPartOnRay
method, raycasting is still sufficiently slow to be avoided when not necessary.
ROBLOX has Ray values, which are used by the engine to represent the origin and direction of rays. These rays can be created with the Ray.new
function:
Ray.new(origin, direction)
Parameter | Type | Description |
---|---|---|
ray | Ray | The ray value which represents the origin and the direction in which the ray should be cast. |
ignoreDescendentsInstance | Instance | An object of which the descendants will be ignored by the ray. |
terrainCellsAreCubes | boolean | If this is set to true, the shape of terrain cells will be ignored and they will be considered as cubes. |
The method, which is a method of the Workspace, returns two values: the part the ray hit, if it hit one, and the point at which the part was hit.
There is also another method that is almost identical to the FindPartOnRay
method, the FindPartOnRayWithIgnoreList
method, but with the difference that instead of having a parameter to ignore all the descendants of an object, it ignores all the descendants of objects in a table. This allows further customization of the parts that are ignored but is also slower.