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

01Mouse.Button1Down:connect(function()
02 
03 
04    print("Firing")
05 
06 
07    local bullet = Instance.new("Part")
08 
09 
10    bullet.Size = Vector3.new(0.10,0.2,0.2)
11 
12 
13    bullet.BrickColor = BrickColor:random()
14 
15 
View all 22 lines...

end)

0
just do bullet.Velocity = Vector3.new(putSomethingHere) theCJarmy7 1293 — 9y
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 — 9y

1 answer

Log in to vote
1
Answered by 9 years ago
01Mouse.Button1Down:connect(function()
02    print("Firing!")
03    local bullet = Instance.new("Part", workspace) --Creates the instance in workspace
04    bullet.Size = Vector3.new(.1, .2, .2) --The same size you wanted just made more simpler
05    bullet.BrickColor = BrickColor:random()
06    local pos = Tool:WaitForChild("Barrel").Position --Added WaitForChild so it won't run script without loading this!
07    local focus = mouse.Hit.p
08    bullet.CFrame = CFrame.new(pos, focus) --CFrame is more efficient than Position
09    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
10    local v = Instance.new("BodyVelocity",bullet)
11    v.Velocity = bullet.CFrame.lookVector * 500 --Change to whatever number you want(how fast you want it)
12    v.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
13end)

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 — 9y
Ad

Answer this question