I’m trying to add spread to my projectile weapon, so that it’s shots are not pinpoint accurate.
My weapon uses the bullet’s LookVector to put force into a BodyVelocity, I’m trying to use a random number to change each of the bullets’ Orientation values so that the LookVector changes as well. I have the random numbers set up, the only line I’m having trouble with is where I change the bullet’s Orientation.
My code:
-- server
local function shoot(player, mouseHit, bloom)
local randXbloom = math.random(-bloom,bloom)
local randYbloom = math.random(-bloom,bloom)
local randZbloom = math.random(-bloom,bloom)
local b = Instance.new("Part",workspace)
b.CFrame = player.Character.Rifle.BulletPos.CFrame
b.Size = Vector3.new(0.6,0.6,0.6)
b.CFrame = CFrame.new(b.Position,mouseHit.p)
--this line \/
b.Orientation = Vector3(b.Orientation.X+randXbloom,b.Orientation.Y+randYbloom,b.Orientation.Z+randZbloom) --this is the line I need help with
local BV = Instance.new("BodyVelocity",b)
BV.Velocity = b.CFrame.LookVector * 255
b.Name = "Bullet"
b.CanCollide = false
b.Touched:Connect(function(hit)
if hit.Name ~= "Bullet" then
local human = hit.Parent:FindFirstChild("Humanoid")
if human then
if hit.Parent ~= player.Character and hit.Parent ~= player.Character.Rifle then
human:TakeDamage(9)
end
end
if hit.Parent ~= player.Character and hit.Parent ~= player.Character.Rifle and hit.CanCollide == true then
local e = Instance.new("Part", workspace)
e.Position = b.Position + Vector3.new(0,0.2,0)
b:Destroy()
e.Name = "Bullet"
e.Shape = "Ball"
e.Size = Vector3.new(0.55,0.55,0.55)
e.CanCollide = false
e.Anchored = true
e.Transparency = 0.5
e.BrickColor = BrickColor.new("Toothpaste")
e.Material = Enum.Material.Neon
for i = 1, 10, 1 do
e.Size = e.Size + Vector3.new(0.2,0.2,0.2)
e.Transparency = e.Transparency + 0.05
wait(0.025)
end
game:GetService("Debris"):AddItem(e)
end
end
end)
b.BrickColor = BrickColor.new("Toothpaste")
b.Material = Enum.Material.Neon
b.Shape = "Ball"
end
script.Parent.WeaponLocal.Shoot.OnServerEvent:Connect(shoot)
--client
local function shoot()
if fire and canFire then
canFire = false
script.ShootAuto:FireServer(mouse.Hit,bloom)
wait(0.13)
canFire = true
end
end
mouse.Button1Down:Connect(function()
fire = true
end)
mouse.Button1Up:Connect(function()
fire = false
end)