Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make shotgun spread?

Asked by 5 years ago

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

1 answer

Log in to vote
0
Answered by 5 years ago

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

0
Thanks but it could been Vector3.new(distance,distance,distance) Dalbertjdplayz 37 — 5y
0
yeah, this is just including the randomization aspect of your original post. you can change it to work however you want, this is just an example mustardfoot 90 — 5y
Ad

Answer this question