Raycasting in every direction of a position

I am trying to make a move in my game that can spread fire from a position. To properly spread the fire I need to raycast (to get the raycast normal to angle the fire correctly and to not place it inside walls and stuff) How can I raycast in every direction of a part.

1 Like

In increments of what degrees? Every 5, 1 etc.

1 Like

probably 20 - 30? The fire parts themselves aren’t too small so idk what a good increment would be.

This script shoots a Ray in 8 directions; 7 studs away. I forget how it does diagonals (Knowing me; probably in a very un-mathmatical way}
Also draws the ray for easy debugging…
Canabalize it.

Speed AI by RealAi Inc. - Brains 4 Zombies - Roblox

1 Like

This video explains how to do it pretty well

3 Likes

I did this recently for custom hit detection inside our game (moving from Touched to avoid inconsistencies and give advantage to more difficult to play roles).

I’m no expert with trig, I used to know it, now I just know what I need to know.

What I do is cast out from each default in CFrame matrix (LookVector, RightVector, UpVector, and their inverses), then each default varying in rotation to hit each corner. I then do the same about the x-axis.

Our code is like this, pre-indexing math library at the top of the script (although I’ve heard both it does and doesn’t make a difference, so I’m unsure):

	for i=1,#matrix do

		local b = matrix[i]

		if i == 1 or i == 3 or i == 4 or i == 6 then -- y-axis
			local x,x2 = (b.X * math.cos(90)) + (b.Z * math.sin(90)), (b.X * math.cos(-90)) + (b.Z * math.sin(-90))
			local z,z2 = -(b.X * math.sin(90)) + (b.Z * math.cos(90)), -(b.X * math.sin(-90)) + (b.Z * math.cos(-90))
			local s,s2 = Vector3.new(x,b.Y,z), Vector3.new(x2,b.Y,z2)
			table.insert(matrix,s);table.insert(matrix,s2)
		end

		if i == 2 or i == 5 then -- x-axis
			local y,y2 = (b.Y*math.cos(45)) - (b.Z*math.sin(45)), (b.Y*math.cos(-45)) - (b.Z*math.sin(-45))
			local z,z2 = (b.Y*math.sin(45)) + (b.z*math.cos(45)), (b.Y*math.sin(-45)) + (b.z*math.cos(-45))
			local s,s2 = Vector3.new(b.X,y,z), Vector3.new(b.X,y2,z2)

			table.insert(matrix,s);table.insert(matrix,s2)
		end
	end

Building a table of unit vectors to cast at individually. You’re welcome to steal this code, and then add in rotation about z-axis to fully reach your goal.

We opted for this over angular increments to better cater to rotation.

This code casts to these angles:

image

1 Like

This looks promising but it’s very confusing for me to understand. What exactly is the matrix table? And how would I go about making a raycast with this?

Here is a larger code snippet for your general understanding. These calculations are about HumanoidRootPart.

Matrix table is CFrame’s default unit vectors and their inverses.

1 Like

I’m so happy the community knows of Sebastian Lague. Best dev vlogger

2 Likes

OMG Orb,
I so wish that I had paid attention in school…
And I had no idea that the fibonacci sequence had anything to do with the golden Ratio. Fascinating.

1 Like

For some reason this raycasts in the same direction multiple times.

I found a hacky way to do it.
image
I made a massive sphere out of parts. I then raycast to the lookvector and -lookvector of each part to raycast in every direction. (all the tiny parts are all the spots it will raycast too)

It may not be the best way to do it but it works for me.