Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to create bullet spread?

Asked by 6 years ago

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 1523 — 6y
0
Why he want to random gen bullets of a shotgun? HelperScripting -4 — 6y
0
like when you shoot a shotgun it's a bunch of pellets that are scattered sonicfan0405 18 — 6y

2 answers

Log in to vote
1
Answered by
hellmatic 1523 Moderation Voter
6 years ago
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
Good answer! TdaGreat04 76 — 6y
Ad
Log in to vote
1
Answered by 6 years ago

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!

Answer this question