evasivedocs
Roblox scriptingLuau 0.748 · 24 guides · 385 API entries
Tutorials

Raycasting

A raycast shoots an invisible line through the map and tells you the first thing it hits. The most common use is a visibility check: can you see that player, or is there a wall in the way? This tutorial builds one.

A first ray

workspace:Raycast(origin, direction) starts at origin and travels along direction. The length of direction is how far the ray goes, in studs.

Luau
local cam = workspace.CurrentCamera
local start = cam.Position
local direction = cam.CFrame.LookVector * 500

local hit = workspace:Raycast(start, direction)
if hit then
    print("hit", hit.Instance:GetFullName())
    print("at", hit.Position, "distance", hit.Distance)
    print("surface normal", hit.Normal, "material", hit.Material)
else
    print("nothing within 500 studs")
end

The result has five fields:

Field What it is
Instance The part that was hit
Position Where the ray hit it
Normal The direction the hit surface faces
Distance How far from origin the hit is
Material The part's material, as an Enum.Material item

nil means the ray hit nothing. A ray can be up to 15000 studs long.

Leaving things out

When the camera sits inside your own character, the first thing a ray hits is often your own body. RaycastParams tells the ray what to skip:

Luau
local Players = game:GetService("Players")
local me = Players.LocalPlayer

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = { me.Character }

local camera = workspace.CurrentCamera
local result = workspace:Raycast(camera.Position, camera.CFrame.LookVector * 500, params)
print(result and result.Instance.Name or "nothing")

With Exclude, the ray passes through everything in the list and everything inside it. With Include, it can only hit those things. params:AddToFilter(object) adds to the list later.

A visibility check

To check whether a player is visible, cast from the camera to a body part. If the first thing the ray hits belongs to that player's character, nothing is in the way.

Luau
local Players = game:GetService("Players")
local me = Players.LocalPlayer

local function isVisible(character, part)
    local params = RaycastParams.new()
    params.FilterType = Enum.RaycastFilterType.Exclude
    params.FilterDescendantsInstances = { me.Character }

    local origin = workspace.CurrentCamera.Position
    local result = workspace:Raycast(origin, part.Position - origin, params)
    return result == nil or result.Instance:IsDescendantOf(character)
end

for _, player in ipairs(Players:GetPlayers()) do
    local character = player ~= me and player.Character
    local head = character and character:FindFirstChild("Head")
    if head then
        print(player.Name, isVisible(character, head) and "visible" or "behind something")
    end
end

The direction is part.Position - origin, so the ray stops right at the part. If the ray gets all the way there without hitting anything, the result is nil, which also counts as visible.

Coloured boxes by visibility

Here is the check running every frame, turning a box green when you can see the player and red when you cannot. It reuses the box idea from Drawing on screen.

Luau
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local me = Players.LocalPlayer

local frames = {}
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude

local SEEN = Color3.fromRGB(80, 255, 120)
local HIDDEN = Color3.fromRGB(255, 70, 70)

local function visible(character, part, origin)
    local result = workspace:Raycast(origin, part.Position - origin, params)
    return result == nil or result.Instance:IsDescendantOf(character)
end

RunService.RenderStepped:Connect(function()
    params.FilterDescendantsInstances = { me.Character }
    local origin = workspace.CurrentCamera.Position
    local present = {}

    for _, player in ipairs(Players:GetPlayers()) do
        if player ~= me then
            present[player] = true
            local frame = frames[player]
            if not frame then
                frame = Drawing.new("Square")
                frame.Thickness = 2
                frames[player] = frame
            end

            local character = player.Character
            local root = character and character:FindFirstChild("HumanoidRootPart")
            local head = character and character:FindFirstChild("Head")
            local top, topOn, bottom, bottomOn
            if root and head then
                top, topOn = WorldToScreen(root.Position + Vector3.new(0, 3, 0))
                bottom, bottomOn = WorldToScreen(root.Position - Vector3.new(0, 3.5, 0))
            end

            local shown = topOn and bottomOn
            if shown then
                local height = bottom.Y - top.Y
                frame.Size = Vector2.new(height * 0.6, height)
                frame.Position = Vector2.new(top.X - height * 0.3, top.Y)
                frame.Color = visible(character, head, origin) and SEEN or HIDDEN
            end
            frame.Visible = shown == true
        end
    end

    for player, frame in pairs(frames) do
        if present[player] == nil then
            frame:Remove()
            frames[player] = nil
        end
    end
end)

The last loop removes the box of anyone who has left the server.

What a ray can see

evasive keeps its own copy of the map's shapes to cast against, so a ray behaves a little differently from one inside the game:

  • Hit: anchored parts by their real shape, loose parts by their box, and every character's body parts by their boxes.
  • Not hit: Terrain, parts with Transparency of 0.5 or more, parts with CanCollide turned off, and some MeshParts and unions that have no collision shape.

For visibility checks this is usually what you want. Glass and invisible walls do not block your view, and characters are easy to hit.

The first ray after the map loads can come back nil for up to half a second while evasive builds its copy of the map. That only happens when evasive's own wall check and radar are both off.

A ray costs about 15 microseconds from a script, so casting at every player every frame is fine.