When I click, the bullet comes out all kinds of different ways. I don't know why it does this, but it does. I just want it to fly straight forward.
function fireBullet(d) local bullet = Instance.new("Part") bullet.FormFactor = "Custom" bullet.Size = Vector3.new(1.5, 0.35, 0.35) bullet.BrickColor = BrickColor.new("White") bullet.Position = tool.Handle.Position + (d * 5) bullet.Velocity = d * 125 local s = script.RemoveBullet:Clone() s.Disabled = false s.Parent = bullet local force = Instance.new("BodyForce") force.force = Vector3.new(0, 25, 0) force.Parent = bullet bullet.Parent = game.Workspace end
Neither of these answers are correct.
The direction it flies all depends on what you give the parameter d
when you call the function. Since we don't have the code where the function is called, we cannot tell for sure what the problem is.
When I made guns, I usually correctly CFrame the bullet first,
--Point towards mouse bullet.CFrame = CFrame.new(tool.Handle.CFrame.p, mouse.Hit.p) --Point forwards from the handle bullet.CFrame = CFrame.new(tool.Handle.CFrame.p)
then I make it fly forwards according to its lookVector.
(where the part is "looking"; its Front Face.)
bullet.Velocity = bullet.CFrame.lookVector * 125
Since I do not know what d
ends up standing for, I do not know specifically what you're doing wrong. However, I can tell you for sure that speed and BodyForces will not affect how straight the bullet flies.
bullet.Velocity = d * 125
should b like
bullet.Velocity = d * 400
the more you add to the last number, the faster it goes:)
You need to make the Force (0,100,0) That makes a perfectly stright shot.
So like this
function fireBullet(d) local bullet = Instance.new("Part") bullet.FormFactor = "Custom" bullet.Size = Vector3.new(1.5, 0.35, 0.35) bullet.BrickColor = BrickColor.new("White") bullet.Position = tool.Handle.Position + (d * 5) bullet.Velocity = d * 125 local s = script.RemoveBullet:Clone() s.Disabled = false s.Parent = bullet local force = Instance.new("BodyForce") force.force = Vector3.new(0, 100, 0) --This makes it perfectly stright force.Parent = bullet bullet.Parent = game.Workspace end