Mimic Random Bullet Accuracy Not Constant

  1. What do you want to achieve?

I want to create an accuracy spread for my gun to mimic where the higher the number, the worse the accuracy becomes.

  1. What is the issue?

The current issue is that my bullet becomes less accurate the close the enemy is at. I want it so that my accuracy spread is constant regardless of the distance of the enemy.

image

Noticed how I aim outside of the part which gave a much more accurate spread compared to if I aim at the block shown on the right. (I know that mouse.hit.p is affecting this)

  1. What solutions have you tried so far?

I attempted to filter out the workspace with TargetFilter for the mouse. However, this causes the bullet to be offset from the original crosshair, which may be off-putting for new players that are playing my game for the first time.

Current accuracy system:

local bulletPosition = mouse.Hit.p + Vector3.new(-ACCURACY_PENALTY + math.random() * ACCURACY_PENALTY * 1.5, -ACCURACY_PENALTY + math.random() * ACCURACY_PENALTY * 1.5, -ACCURACY_PENALTY + math.random() * ACCURACY_PENALTY * 1.5)
-- The vector3 mimics the accuracy pattern expected from rng.
-- If there's a better rng system I can use, let me know!
					
cloneBullet.CFrame = script.Parent.Weapon.bulletSpot.CFrame 
-- sets starting position at the barrel of the gun.

cloneBullet.CFrame = CFrame.new(cloneBullet.Position, bulletPosition) 
-- make the bullet face the mouse position

cloneBullet.Velocity = CFrame.new(localPlayer.Character.Head.Position, bulletPosition).lookVector * BULLET_SPEED 
-- adds velocity to bullet to fly towards mouse position

I am thinking that I should create a spread that does not include the mouse.hit.p, but I’m not sure how to go at that.

It would be better to rotate the direction by some random amount to keep it within a kind of cone:

local start = script.Parent.Weapon.bulletSpot.Position
local cf = CFrame.new(start, mouse.Hit.p).Rotation

-- rotate first on Z by a random 0-360 degrees
cf = cf * CFrame.Angles(0, 0, 2*math.pi*math.random())

-- tilt X by some amount
local CONE_ANGLE = 15 -- degrees
cf = cf * CFrame.Angles(math.random() * math.deg(CONE_ANGLE), 0, 0)

cloneBullet.CFrame = cf + start
cloneBullet.Velocity = cf.LookVector * BULLET_SPEED
2 Likes

Thanks for your suggestion! However, my bullets are sort of firing all over the place at the moment. I am a little bad with angles, could you explain how I could modify the cone to make the shots more consistent?

Wait actually, I figured out how to use the degree to make an angle. I’m just surprised that I need to use a minimum of 0.005 as my cone angle to create a visible cone, which I don’t understand why, but it works :man_shrugging:

Ah. Switch the math.deg to a math.rad. Then the CONE_ANGLE will mean “number of degrees to deflect from the center”