You can define a spread look vector with an angle of n by doing:
local origin = game.Players.LocalPlayer.Character.Head.Position
local dir = (mouse.Hit.p - game.Players.LocalPlayer.Character.Head.Position).unit
local cf = CFrame.new(origin,origin + dir)
* CFrame.Angles(0,0,math.random()*math.pi*2)
* CFrame.Angles(n*(math.random()*2-1) ,0,0)
local lookVector = cf.LookVector
local range = settings.gunSettings.range
local ray = Ray.new(origin, lookVector*range)
You can probably replace n with chosenSPREAD and be fine. This snippet should give you a look vector in the direction you want it to go to, while adding a random spread with respect to max angle n.
Also, I’m assuming you want everything in between of -1 and 1 when getting the spread instead of just one or the other, so I suggest doing math.random()*2-1 instead of math.random(-1,1) when solving for a random number between -1 and 1.
n is in radians, not angles. If your input is angles you can just do math.rad(n)
The last CFrame has the value n in it, which makes it the CFrame that inflicts the spread. The second CFrame rotates it randomly around another axis that doesn’t interfere with spread. You don’t need to worry about that one.
Wouldn’t it constantly be fighting with the spread increasing / decreasing?
Because if you stop shooting, then fire again, you’re going to have one instance trying to increase it while the other is trying to decrease it, making it jump between the two repeatedly, at least, in my experience.
local spread = 1
local canSpread = true
local function setSpread(amount, increment)
if canSpread == true then
for i = 1, amount, increment do
spread = i
end
end
end
Control canSpread however youd like. (Sorry I’m 3 hours late, had to do some stuff.