Best method for incrementing rotations in scripting

In a nutshell, I am making a object fire rays at 30-degrees increments on the Y axis for hit detection (30 → 60 → 90… → 360), and it has occurred to me that this algorithm is significantly more complex that what I originally thought it would be.

local RayDirection = -30


	if side == 0 then -- DW about this part its for smt else


		while RayDirection < 360 do
			
			RayDirection += 30
	
			local HitRay = workspace:Raycast(Part.Position, Vector3.new(90, RayDirection, 0))
	
			if HitRay then
				print(HitRay.Instance)
			end

		end
else

After testing around with rotations for a bit, I realized that the other axis also change during rotations. Having Y go high or low enough without changing the other rotation variables seems to halt any further, as shown by the output.

Is there any simple way to increment a axis of rotation in scripting?

What you are doing right now is Vector3.new(90, RayDirection, 0), this just increases the length of ray along Y axis, this doesn’t rotate the vector as you think so. Just play around with CFrames.

local Base = CFrame.new()
local RayLength = ...
...

while RayDirection < 360 do
			
	RayDirection += 30
	
	local HitRay = workspace:Raycast(Part.Position, (Base* CFrame.Angles(0,math.rad(RayDirection),0)).lookVector * RayLength)
	
	if HitRay then
		print(HitRay.Instance)
	end

end

Change base cframe as you like, right now it is along x-y plane.

Testing math
--ServerScript in ServerScriptService
local part = workspace:WaitForChild("Part")
local dist = 90

for angle = 0,330,30 do
	local dir = (CFrame.Angles(0, math.rad(angle), 0).LookVector) * dist
	local p = Instance.new("Part")
	p.Size = Vector3.new(0.2,0.2,dist)
	p.Anchored = true
	p.CanCollide = false
	p.CFrame = CFrame.lookAt(part.Position, part.Position + dir) * CFrame.new(0,0,-dist/2)
	p.Parent = workspace
end
LocalScript hit test
--LocalScript in StarterPlayerScripts
local part = workspace:WaitForChild("Part")
local dist = 200

while true do
	task.wait(0.1)
	for angle = 0,330,30 do
		local dir = CFrame.Angles(0, math.rad(angle), 0).LookVector * dist
		local hit = workspace:Raycast(part.Position, dir)
		if hit then
			print(hit.Instance:GetFullName())
		end
	end
end

Math testing outcome:


The hit test also worked.. Did you even look at this?

The Raycast is returning with nil

Also why do ray work like this? The mathematical definition of a angle is a point of origin and a slope or angle. Considering both position and slope can be easily represented with integers, I struggle to understand why a computer would have to rely on CFrames to calculate a line.

Here’s what your code is doing.
You initially set RayDirection to 0 in your while loop, so this is what happens:

Then, you add 30 studs to the y-component of the vector:

Then you add another 30 upwards (you don’t rotate the vector by 30 degrees; you’re just making it point more upwards by 30 studs):

What happens when RayDirection = 330:

What do you mean by this? You can map integers to individual positions, but a position in 3D is a vector because you need one coordinate for every axis. You seem to be mixing up concepts.

CFrames (short for CoordinateFrame) represent coordinate systems, but they also represent transformations like translation or rotation (something you learn in linear algebra and computer graphics). In this case you use CFrames to rotate the direction of the raycast. For example, you can rotate a vector about the x-axis by applying a rotation transformation:

local part = workspace.Part -- any part
local v = Vector3.new(0, 0, -1)
local angle = 30 -- degrees
local oneStudInFont = part.CFrame * v -- this is the position 1 stud in front of the part
local rotatedVector = CFrame.Angles(math.rad(angle), 0, 0) * v -- this just means "rotate the vector v by 30 degrees about the world x-axis"
local rotatedVectorRelativeToPart = part.CFrame * CFrame.Angles(math.rad(angle), 0, 0) * v -- this means "take the vector v, rotate it by 30 degrees, then translate the vector so that it is relative to the part's coordinate frame (object space of part)"

These are just some examples. In @Razor_IB’s example, the approach looks a bit different, but it’s following the same idea.

Example

Here is an example of how to spawn a circle of parts around another part:

local angle = -30
local p = workspace.Part -- red part in screenshot

while angle < 360 do
	local clone = p:Clone()
	clone.Color = Color3.new(0, 1, 0)
	clone.Position = p.CFrame * CFrame.Angles(math.rad(angle), 0, 0) * Vector3.new(0,0,5)
	clone.Parent = workspace
	angle += 30
	wait(1)
end

No matter how the red part is orientated, the circle of parts will always spawn relative to its position and orientation. Here I rotated the red part to show how the ring follows its orientation:

Oh it is using slop (or some form of it).

I’m gonna incorporate this into the code GMAS

Thx btw

Ok it worked thanks for the help <3