How would i use math to make a rectangular raycast

Cast multiple raycasts starting from an origin (middle cast) and there’s casts on left and right

Like this:

1 Like
local CF = Origin -- Your origin point
local Dir = Vector3.new(100, 0, 0) -- The direction of the raycast
local Spacing = 3 -- The space between each point

for i = -3, 3 do
	local Multiplier = (i > 0) and 1 or -1 -- Used to define the direction
	local Pos = (
        (CF * CFrame.Angles(0, 90 * Multiplier, 0)) -- Make the Coordinate point turns to the side
        + CF.LookVector * i * Spacing) -- Move by the specified spacing times by i
        .Position -- Then get the position to be used for the raycasting
	local RayCast = workspace:Raycast(Pos, Dir) -- Raycasting!
end

For the explanation, the for loop runs from -3 to 3 (could be in any range), then the Pos is calculated by rotating the origin point by 90 degrees in the y-direction (Direction is changed by the multiplier), then moving by a distance of i * Spacing in CF.LookVector . The .Position is then used to get the position to be used for the raycasting.

Hope this helps! :grin:

1 Like

Would workspace:BlockCast() work?

thank you so much. the comments are really helpful

yeah but really expensive on performance and my raycasts are like 1 stud apart so it has high accuracy (for what i’m trying to do)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.