Still have questions? Join our Discord server and get real time help.
1

# How to create bullet spread?

I'm making a shotgun and I'm trying to get the bullets to be randomly generated in the direction the player is pointing at. I can get the bullet to go in the direction of the mouse but I cannot manipulate this value and that's where I'm stuck. I'm kind of new to this and I'm not sure where to go from here

Code so far:

local tool = script.Parent
tool.Name = "Shotgun"
local player = game.Players.LocalPlayer
local char = player.Character
local mouse = player:GetMouse()
----------------------------------------------

function GetBullet()
local p = Instance.new("Part", workspace)
p.FormFactor = "Custom"
p.Size = Vector3.new(1, 1, 1)
p.BrickColor = BrickColor.Black()
p.Reflectance = 0.5
p.CanCollide = false
Instance.new("BodyVelocity", p)
return p
end

local function onEquip()

end

local function onActivate()
local bullet = GetBullet()
local target = mouse.hit
bullet.CFrame = char.Shotgun.Barrel.CFrame
bullet.CFrame = CFrame.new(bullet.Position, target.p)   --this is where im stuck
bullet.BodyVelocity.Velocity = bullet.CFrame.lookVector * 50

end

tool.Equipped:connect(onEquip)
tool.Activated:connect(onActivate)

1
use raycasting (easier, phantom forces uses it) hellmatic 1517 — 2y
0
Why he want to random gen bullets of a shotgun? HelperScripting -4 — 2y
0
like when you shoot a shotgun it's a bunch of pellets that are scattered sonicfan0405 18 — 2y

1
hellmatic 1517
2 years ago
local function onActivate()
local bullet = GetBullet()
local target = mouse.hit

bullet.CFrame = char.Shotgun.Barrel.CFrame
bullet.CFrame = CFrame.new(bullet.Position, target.p) * Spread

bullet.BodyVelocity.Velocity = bullet.CFrame.lookVector * 50
end

1
Good answer! TdaGreat04 76 — 2y
1

For this, we can use CFrame.Angles and math.rad!

CFrame.Angles is used to make angles, and math.rad gives us the degrees we need for rotation!

local spread = 30 -- The maximum amount of degrees the bullets will spread, change this to your need

local function onActivate()
local bullet = GetBullet()
local target = mouse.hit
bullet.CFrame = char.Shotgun.Barrel.CFrame
bullet.CFrame = CFrame.new(bullet.Position, target.p)
if math.random(1,2) == 1 then --I do this to give a 50% chance that the CFrame is inverted
bullet.CFrame = bullet.CFrame * CFrame.Angles(math.rad(math.random(0, spread)), 0, 0) --Generate random angle and apply it
else
bullet.CFrame = bullet.CFrame * CFrame.Angles(math.rad(math.random(0, spread)), 0, 0):inverse() --Generate random angle, inverse it, then apply it
end
if math.random(1,2) == 1 then --I do this to give a 50% chance that the CFrame is inverted
bullet.CFrame = bullet.CFrame * CFrame.Angles(0, math.rad(math.random(0, spread)), 0) --Generate random angle and apply it
else
bullet.CFrame = bullet.CFrame * CFrame.Angles(0, math.rad(math.random(0, spread)), 0):inverse() --Generate random angle, inverse it, then apply it
end
if math.random(1,2) == 1 then --I do this to give a 50% chance that the CFrame is inverted
bullet.CFrame = bullet.CFrame * CFrame.Angles(0, 0, math.rad(math.random(0, spread))) --Generate random angle and apply it
else
bullet.CFrame = bullet.CFrame * CFrame.Angles(0, 0, math.rad(math.random(0, spread))):inverse() --Generate random angle, inverse it, then apply it
end
bullet.BodyVelocity.Velocity = bullet.CFrame.lookVector * 50
end


I hope this helped!