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)
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
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!