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

How do you add Velocity to a bullet?

Asked by 8 years ago

I have made a basic gun and so far, i can spawn a little bullet of random color, but no matter what i do, I cannot make this bullet have any velocity to it. I have tried looking at many different guides and tutorials to no success, much help is appreciated. Here is the script:

local Tool = script.Parent

local debounce = false

Tool.Equipped:connect(function(Mouse)

Mouse.Button1Down:connect(function()


    print("Firing")


    local bullet = Instance.new("Part")


    bullet.Size = Vector3.new(0.10,0.2,0.2)


    bullet.BrickColor = BrickColor:random()


    bullet.Position = Tool.Barrel.Position


    bullet.Parent = game.Workspace


end)

end)

0
just do bullet.Velocity = Vector3.new(putSomethingHere) theCJarmy7 1293 — 8y
0
Thank you sir, I have looked at so many guides and i think i was getting confused with velocity and mouse direction, mouse cframe and all that. After a week of working on this, my gun finally works so I thank you so much for sending me this. :P Sparkflyer34 40 — 8y

1 answer

Log in to vote
1
Answered by 8 years ago
Mouse.Button1Down:connect(function()
    print("Firing!")
    local bullet = Instance.new("Part", workspace) --Creates the instance in workspace
    bullet.Size = Vector3.new(.1, .2, .2) --The same size you wanted just made more simpler
    bullet.BrickColor = BrickColor:random()
    local pos = Tool:WaitForChild("Barrel").Position --Added WaitForChild so it won't run script without loading this!
    local focus = mouse.Hit.p
    bullet.CFrame = CFrame.new(pos, focus) --CFrame is more efficient than Position 
    bullet.CFrame = bullet.CFrame + bullet.CFrame.lookVector * 5 -- Get's the way the tool is facing and creates the bullet in front it. you can change number for how far you want the bullet to be when it is created
    local v = Instance.new("BodyVelocity",bullet)
    v.Velocity = bullet.CFrame.lookVector * 500 --Change to whatever number you want(how fast you want it)
    v.MaxForce = Vector3.new(math.huge, math.huge, math.huge) 
end)

Hope this works out for you. You can change the the numbers around to suit your needs.

0
Tried the script, had to tweak it a bit. Didn't have a local for Tool and needed a function for tool.equipped, but once I added those in the script, it worked like a charm and i absolutely love it! I have so much control of the bullet, how it is , how fast it goes, where it spawns etc. Thank you so much for this script, I can now finish my very first gun! :D Sparkflyer34 40 — 8y
Ad

Answer this question