Shotgun spread problem


Here is a rough sketch of why that occurs.
Notice how the angle offset from the shot going towards your mouse gets smaller with greater distance.
This is what causes it to shoot more accurately at a distance (as Vector3.new(R1, R2, R3) does not change with distance).

You could use what @Locard suggested (within the similar post linked), however, this suffers from one minor problem (if what you want is even spread), Disk Point Picking -- from Wolfram MathWorld. You’ll end up with the spread pattern on the left, rather than the one on the right.

If you want a spread pattern similar to what is shown on the right, use this instead

local rng_v = Random.new()

function RandomVectorOffset(v, maxAngle) --returns uniformly-distributed random unit vector no more than maxAngle radians away from v
    return (CFrame.new(Vector3.new(), v)*CFrame.Angles(0, 0, rng_v:NextNumber(0, 2*math.pi))*CFrame.Angles(math.acos(rng_v:NextNumber(math.cos(maxAngle), 1)), 0, 0)).LookVector
end 

maxAngle just represents the maximum angular offset between the accurately shot vector and the spread-away vector.

Putting this all together with a max angular offset of about 30 deg, you get,

local dir = RandomVectorOffset((mouse.Hit.p - ShootPart.CFrame.p).Unit, math.rad(30)) 
local ray = Ray.new(ShootPart.CFrame.p, dir * ShotgunConfigs.Range.Value)
16 Likes