Im adding a shotgun mode to my guns and heres one problem: I want to make the shotgun spread depending how long the range is and it looks too big when you shoot somewhere closer to you. heres the code
for v = 1,(Config.ShotgunMode and Config.BulletsPerShot or 1) do local ShotgunSpreadX = Config.ShotgunMode and math.random(-5,5) or 0 local ShotgunSpreadY = Config.ShotgunMode and math.random(-5,5) or 0 local ShotgunSpreadZ = Config.ShotgunMode and math.random(-5,5) or 0 local BulletRay = Ray.new(Tool.Handle.BulletStart.WorldCFrame.p,(Mouse.Hit.p + Vector3.new(ShotgunSpreadX,ShotgunSpreadY,ShotgunSpreadZ)-Tool.Handle.BulletStart.WorldCFrame.p).unit*Config.Range) --Theres more code; I dont want to show it
You could take the magnitude (distance) between the start point and the end point (likely the gun barrel and the mouse's position) with the code (position1 - position2).magnitude
. Then you'd divide the magnitude by something like 50 (the higher the number the lower the spread will be). After that, just multiply the random values for spread by this number and you'll have spread based on distance. For example,
local distance = (Tool.Handle.BulletStart.Position - Mouse.Hit.p).magnitude / 100
for v = 1,(Config.ShotgunMode and Config.BulletsPerShot or 1) do
local ShotgunSpreadX = Config.ShotgunMode and math.random(-5,5) or 0
local ShotgunSpreadY = Config.ShotgunMode and math.random(-5,5) or 0
local ShotgunSpreadZ = Config.ShotgunMode and math.random(-5,5) or 0
local BulletRay = Ray.new(Tool.Handle.BulletStart.WorldCFrame.p, (Mouse.Hit.p + Vector3.new(ShotgunSpreadX * distance, ShotgunSpreadY * distance, ShotgunSpreadZ * distance) - Tool.Handle.BulletStart.WorldCFrame.p).unit * Config.Range)
end