Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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)

3 comments

1

use raycasting (easier, phantom forces uses it)

0

Why he want to random gen bullets of a shotgun?

0

like when you shoot a shotgun it's a bunch of pellets that are scattered

2 answers

✓ Accepted answer
local function onActivate()
	local bullet = GetBullet()
	local target = mouse.hit
	
	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))


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

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

1 comment

1

Good answer!

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.