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

Bullet Spread on Projectile Weapons?

Asked by 5 years ago
Edited 5 years ago

This is my first question on this site, I making a Projectile based gun and it is way too accurate so I want to add bullet spread but I can't get it to work. Where do I put the bullet spread part?

I'm pretty sure this is the full function

function isFiring()
    repeat
    local Bullet = Instance.new("Part",workspace)
    ammo=ammo-1
    print(ammo)
    function onTouched(hit)
    if hit.Parent.Name==(player.Name) then
    hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - Damage*0
    elseif hit.Name == "Head" then
    hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - Damage*2
    Bullet:Destroy()
    elseif hit.Name == "Torso" then
    hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - Damage*1
    Bullet:Destroy()
    elseif hit.Name == "Right Leg" then
    hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - Damage*0.5
    Bullet:Destroy()
    elseif hit.Name == "Left Leg" then
    hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - Damage*0.5
    Bullet:Destroy()
    elseif hit.Name == "Right Arm" then
    hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - Damage*0.5
    Bullet:Destroy()
    elseif hit.Name == "Left Arm" then
    hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - Damage*0.5
    Bullet:Destroy()
    elseif hit:IsA("Hat") then
    hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - Damage*math.random(0.25,2)
    Bullet:Destroy()
    end
    end
    if ammo == 0 or Reloading then
        Firing=false
    end
    --Bullet--
    game.Debris:AddItem(Bullet,1)
    Bullet.Name= "Bullet"
    Bullet.CanCollide= false
    Bullet.Shape = "Block"
    Bullet.Material = "Neon"
    Bullet.BrickColor = BrickColor.new ("Bright yellow")--Colour--
    Bullet.Size=Vector3.new (0.1,0.1,2.5)--Size--
    Bullet.CFrame = Barrel.CFrame*CFrame.new(0,0,0)--Where--
    Bullet.CFrame= CFrame.new(Bullet.Position,mouse.hit.p)
    local Velo = Instance.new ("BodyVelocity",Bullet)
    Velo.velocity =  Bullet.CFrame.lookVector*200--Speed of Bullet--
    Velo.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
    Bullet.CustomPhysicalProperties = PhysicalProperties.new(0.01,0,0,0,0)  
    local weld =Instance.new("Weld",Bullet)
    weld.C0= Bullet.CFrame:inverse()*Bullet.CFrame
    weld.Part0=Bullet
    weld.Part1=Bullet
    local a0 = Instance.new("Attachment")
    a0.Parent = Bullet
    a0.Position = Vector3.new(0,.5,0)
    a0.Name = "Trail Attachment 0"
    local a1 = Instance.new("Attachment")
    a1.Parent = Bullet
    a1.Position = Vector3.new(0,-.5,0)
    a1.Name = "Trail Attachment 1"
    local trail = Instance.new("Trail")
    trail.Parent = Bullet
    trail.Attachment0 = a0
    trail.Attachment1 = a1
    trail.Transparency = NumberSequence.new(0.5,1)
    trail.WidthScale=NumberSequence.new(0.1,0.1)
    trail.Color = ColorSequence.new(Color3.new(205,150,0))
    trail.LightEmission=0.75
    trail.Lifetime = 0.5
    Bullet.Touched:Connect(onTouched)
    wait(.075)--FireRate--
    enabled= true
    --Bullet--
    until Firing == false or Ammo == 0
end


0
can you post the whole function here, I'd like a bit more information before I try to answer your question. SteamG00B 1633 — 5y

1 answer

Log in to vote
0
Answered by
Vathriel 510 Moderation Voter
5 years ago
Edited 5 years ago

So we can think of this as a rotational problem.

if we say our direction is something like this from the wiki: local rotation = Bullet.CFrame - Bullet.Position then we can also change each component by a possible spread value.

local spread = 5 --I chose this randomly you may need to play with this.
local rotation = Bullet.CFrame - Bullet.Position
rotation = rotation + CFrame.Angles(math.random(-spread,spread),math.random(-spread,spread),math.random(-spread,spread))

--and finally we have to recombine them

Bullet.CFrame = Bullet.Position + rotation

--Then our velocity should be the look vector of this new CFrame.

Velo.velocity = Bullet.CFrame.lookVector*200

That should work, I hope I understood your question. If there are mistakes let me know and I'll fix them in the morning :)

0
can u subtract a Vector3 from a CFrame?? Gey4Jesus69 2705 — 5y
0
Yep. For this use case you can. I’ll send over the wiki page when I get back to my laptop Vathriel 510 — 5y
0
Keep getting this Players.SWARMHOST77.Backpack.Gun.Shoot:74: bad argument #2 to '?' (Vector3 expected, got CFrame) SWARMHOST77 12 — 5y
View all comments (2 more)
0
Doesn't really help without seeing what's around that code. Vathriel 510 — 5y
0
Line 74 in your posted code is a repeat loop which doesn't at all seem like what the error is coming from. Vathriel 510 — 5y
Ad

Answer this question