I have made a script that applies spread to a bullet but the problem here is I want it based on your character’s distance to the closest object infront. It should work like this:
The further away the object the more spread becomes, it can’t go beyond already specified spread
The closer the object is the more accurate the bullets are
Currently it just makes the further away objects have better accuracy than the objects closer to you
local Hit = Utility:CastRay(CameraRay.Origin, CameraRay.Direction * 2500, false, IgnoreList)
local Position = CameraRay.Direction * 25000
if Hit then
Position = Hit.Position - CameraRay.Origin
end
local Magnitude = (WeaponModel.Muzzle.Position - Position).Magnitude
Position = Position + Vector3.new(Magnitude * (math.random(-GunSetting.Spread, GunSetting.Spread) / 600), (Magnitude * (math.random(-GunSetting.Spread, GunSetting.Spread)) / 600), (Magnitude * (math.random(-GunSetting.Spread, GunSetting.Spread) / 600)))
Remotes.Guns.ReplicateBullet:FireServer(Position)
local Cast = caster:Fire(WeaponModel.Muzzle.Position, (CameraRay.Origin + Position) - WeaponModel.Muzzle.Position, 500, castBehaviorLocal)
Cast.UserData.Local = true