How i can do RayCast algorithm?

I wanna make algorithm of raycasting that works like:
We got origin from, like o(0, 0, 0) to d(1000, 0, 0).
And in roblox we can’t make that big raycast, because of lag.

So, i wanna make many raycasts, like 4, each to 250 studs
And i wanna make something like that:
We go full 250 studs first. then again.
If we hit nothing, we go on half back, 250/2 = 125 studs back
And make ray on 125 studs again.
Until ray will very very small.

How i can code it?


Perhaps this may be useful to you:

This isn’t very practical in roblox because we don’t get low level access to say the individual polygons or anything like that. There isn’t really any fast option in roblox that would ever perform any better than the built in ray-casting system.

I wanna use workspace:Raycast() multiply times

You can.
Documentation for it is in WorldRoot:Raycast .

Yes, i know, but how i can calculate vectors for algorithm?

You can probably begin by segmenting the direction in however many pieces you want and then raycasting from a new origin corresponding to how many iterations you’ve done:

local function RayCastInSegments(Origin, Direction, RaycastParams, Segments)
	local Piece = Direction/Segments --If the Segments variable is 4, this will be 1/4 of the direction	

	for i = 1, Segments do
		local result = workspace:Raycast(Origin, Piece, RaycastParams)
		if result ~= nil then -- If something was found on the raycast then return it
			return result 
			--Nothing was found and we need to continue going further
			Origin += Piece --Increment origin to the next raycast

yeees, thats exactly what i need, thanks man!

i just thinking… about more complex vector math… and don’t know how to make that math

1 Like

If you find vector math to be confusing, I recommend you read this post:, which explains it very well.

Okay, thanks, and i find out, it’s not optimized than one long raycast…