Question about this Bullet Spread function

Hey there, so I’ve tried to use bullet spread before but it doesn’t really work as I intend it to.

Function

function spreadBullets(v)
	return Vector3.new(math.random(-v, v), math.random(-v, v), math.random(-v, v) / 100)
end

There are two main questions I have:

  1. How would I make a spread function where the spread gradually gets worse over time as you shoot? (aka Bullet Bloom)
  2. Whenever I input a value less than 1 (ie. .9), there’s no more spread and it comes out as a straight line. What’s the reason behind this?

1.create a spread variable that adds to itself each firing sequence
2. math.random only returns whole numbers if given arguments

Thanks! I’ll tweak with my spread shenanigans and see what I can do.

You can fix the math.random issue by just multiplying the numbers by 10 and dividing the result by 10.

1 Like

Don’t you mean multiplying the range of numbers by 10 (if they need the value of v between 0 and 2, make it 0 and 20), then dividing the result of the math.random(v) by 10?

nooooo please just use r = Random.new(); r:NextNumber(min, max) xD

Have a variable in your shooting-handling-code that gets bigger every time a shot is fired and gradually decreases (every Stepped or Heartbeat or w/e). E.g.

local spread = 0
local SPREAD_INCREASE_PER_SHOT = 1
local SPREAD_MULTIPLIER = 1 --no idea, tune this

function spreadBullets()
    ... SPREAD_MULTIPLIER * Vector3.new(r:NextNumber(-spread, spread), ...)
end

function fire()
    --shooting stuff
    spread += SPREAD_INCREASE_PER_SHOT
end

function spreadDecay(dt)
    --Don't think this is framerate- independent :c 
    --Exponential decay. Tune _DECAY_SPEED and _INCREASE_PER_SHOT to reach a desired equilibrium no. of shots, then tune _MULTIPLIER to get desired spread at that equilibrium. Ask if this makes no sense
   spread *= 1 - (SPREAD_DECAY_SPEED * dt) 
    spread = math.min(spread, 0.05) --snap it to 0 at some point, it'd be weird to still have 0.00000...001 spread 10 minutes after firing a shot :P
end

RunS.RenderStepped:Connect(function(dt)
    --handle other stuff
    spreadDecay(dt)
end)