Help with some basic math

I need some help with some math. I have three varibales “MinimumAngle” and “MaxmimumAngle” and “NumberOfRaycasts”. The trouble is I want to raycast out 10 times from -45 - MinAngle and 45 MaxAngle but I haven’t a clue on how to do this. I have created an image to help express what I mean.


So from Ray Origin I want it to raycast out 9 times like that in the direction of the look vector but with the offset of MinAngle an MaxAngle. How would I get that even spread?

local MinimumAngle = -45
local MaximumAngle = 45
local NumberOfRaycasts = 9

I am not so good with math so help would be very much appreciated form some more experienced. Thanks.

local direction = (part.CFrame * CFrame.angles(math.rad(math.random(MinimumAngle, MaximumAngle)), 0,0)).LookVector

I think this should work. Then just generate the direction 9 times with a for loop or something.

Since there’s a maximum and minimum angle, why not use a single variable called spreadAngle? You only need one variable and add the negative sign in front if you want to negate the value. The way you think about the maximum and minimum angle is based on that one of the sides is the focus rather than the central line cutting through the “cone” or “triangle”.

I don’t want it to be random I need it to be equal linear

I don’t understand what you mean, I have tried making my own equation and then adding a vector3 to the LookVector

local Angle = MinimumAngle + ((MaximumAngle - MinimumAngle) / (NumberOfRaycasts - 1) * Index)

But it didn’t work

local Part = workspace.Part

local MinimumAngle = -45
local MaximumAngle = 45
local NumberOfRaycasts = 9

local raycastParams = RaycastParams.new()

for Index = 0, NumberOfRaycasts - 1 do
	local Angle = MinimumAngle + ((MaximumAngle - MinimumAngle) / (NumberOfRaycasts - 1) * Index)
	local Raycast = workspace:Raycast(Part.Position + Vector3.new(0, 0, Part.Size.Z / 2), (Part.CFrame.LookVector + Vector3.new(Angle, 0, 0)) * 10, raycastParams)
end

As I said i’m not the best at math.

o I didn’t see that part. Ok so get the angle between the max and min angle.

local between = MaximumAngle - MinimumAngle
local step = between/(NumberOfRaycasts  - 1) -- to make the rays start at min angle
for i = 1, NumberOfRaycasts do
    local direction = (part.CFrame * CFrame.Angles(math.rad(MinimumAngle + step * (i-1)),0,0)).LookVector
end

Tried to adjust to some comprehensiveness. This works with symmetrical spreads. And oops.

local Part = workspace.Part

local numberOfRaycasts = 9
local spreadAngle = 45
local totalAngle = spreadAngle * 2
local stepAngle = (totalAngle / (numberOfRaycasts - 1))

for i = 1, numberOfRaycasts do
    local raycastAngle = ((i - 1) * stepAngle) - spreadAngle
    local ray = workspace:Raycast(Part.Position + Vector3.new(0, 0, Part.Size.Z / 2), ((Part.CFrame * CFrame.Angles(math.rad(raycastAngle), 0, 0)).LookVector * 10, raycastParams)
end

Did I forget there is a thing called OBOE?
Off-by-one error - Wikiwand

Thanks for the help, but do you know if I can verify that ray 1 is 45 and ray 9 is -45 (or vise versa) using dot product or some other means just to be sure? Also you did not mention OBOE.

Thanks can you tell me why do you use the word step?

It is how much the angle increases each time. Or are you asking why you have to subtract 1 from the NumberOfRaycasts?

no I mean why is step such a common word to use when doing iterations

Just another word for increment. I’m not to sure where I picked up the practice of using it but it seems pretty common.

You can try to create a part on the ray. I don’t remember the line for it.

Ray API here: