CraftStudio Wiki
Advertisement

The Ray class can be used to check for intersection of a ray with a particular model, map or plane.

Ray:New[]

[Ray] Ray:New( [Vector3] position, [Vector3] direction )

Returns a new ray

Ray:IntersectsPlane[]

[number or nil] Ray:IntersectsPlane( [Plane] plane )

Returns the distance of intersection of the ray with the specified Plane (or nil if there is no intersection)

Ray:IntersectsModelRenderer[]

[number or nil], [Vector3 or nil] Ray:IntersectsModelRenderer( [ModelRenderer] modelRndr )

Returns the distance of intersection of the ray with the specified ModelRenderer (or nil if there is no intersection) and the normal of the hit face.

Example: Computing the hit position[]

-- Setup a ray pointing forward
​local ray = Ray:New( self.gameObject.transform:GetPosition(), Vector3.Transform( Vector3:New( 0, 0, -1 ), self.gameObject.transform:GetOrientation() ) )

-- Assuming we have a model renderer component in "modelRndr", let's cast our ray
local distance = ray:IntersectsModelRenderer( modelRndr )

-- Check if we got a hit
if distance ~= nil then
    -- Compute the position of the hit using the distance and print it
    local hitPosition = ray.position + ray.direction * distance
    print( "Hit at " .. hitPosition )
end

Ray:IntersectsMapRenderer[]

[number] distance, [Vector3] normal, [Vector3] hitBlockLocation, [Vector3] adjacentBlockLocation Ray:IntersectsMapRenderer( [MapRenderer] mapRndr )

Returns the distance of intersection of the ray with the specified MapRenderer (or nil if there is no intersection). Additional return values are the normal of the hit face, and the locations in the map of the hit and adjacent map blocks.

Warning: If your ray is at the exact intersection of two map blocks, it will not reliably return the hit distance. As a workaround, you can try avoiding perfectly aligned rays or shooting precisely between two blocks. This should be fixed in a future update.

Advertisement