Simple Bullet spread

I’m aware that there are several other similar topics. However, they all include using the mouse positions, where I’m trying to make it for a turret on a plane. This is the current code I got:

bullet.CFrame = turret.CFrame

This is the formula I’m using:

local SpreadValue = 10
local Spread = CFrame.Angles(math.rad(math.random(-SpreadValue , SpreadValue )/10), math.rad(math.random(-SpreadValue , SpreadValue )/10), math.rad(math.random(-SpreadValue , SpreadValue )/10))

How am I supposed to incorporate them both?

Doing this didn’t do anything:

bullet.CFrame = turret.CFrame * Spread

By getting a random number -10 through 10 and then dividing that by 10, your maximum spread will be 1 degree, which is hardly perceptible, especially when in most cases it is much less that then. Try setting SpreadValue to a higher number, such as 50, so the bullet can be offset by up to 5 degrees in each direction.

For some reason, it’s shooting in a straight line still, but now the bullets rotation is changing cause of the spread.

Are you moving the bullets relative to the turret? They should be moving relative the bullet’s orientation.

Example:

bullet.CFrame = turret.CFrame * Spread
-- example movement thing
for i = 1, 10 do
     bullet.CFrame = bullet.CFrame * CFrame.new(0, 0, -1)
     wait()
end

The bullet is being moved by BodyVelocity:

bullet.BodyVelocity.velocity = (Plane.Turret.CFrame.lookVector*550)

It should be relative to the bullet.

bullet.BodyVelocity.velocity = (bullet.CFrame.lookVector*550)

That will just have the bullets move in a stuck direction. Making it relative to the Turret makes the bullets fire in the direction the plane/turret is facing.

Thats why you orient the bullets based on the turrets position and a randomly generated spread, and then just move the bullet relative to the bullet’s current orientation. The bullet will still appear to be fired from the turret.

3 Likes