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

How would i make my bullets never lose velocity?

Asked by 9 years ago

So i have a noob that shoots a gun when touched, and it works, but i have no idea how to make it so the bullet never loses velocity and falls down to the ground like it usually does

Right now, I have my script set up like this:

head = script.Parent.GunBarrel
function onTouch()
bullet = Instance.new("Part", game.Workspace.NoobWithGun.GunBarrel)
bullet.Name = "Bullet"
bullet.Size = Vector3.new(1,1,1)
bullet.Shape = "Ball"
bullet.CFrame = head.CFrame
bullet.Velocity = head.CFrame.lookVector*1000
bullet.CanCollide = false
bullet.BrickColor = BrickColor.new("Dark stone grey")
end
game.Workspace.NoobWithGun.Torso.Touched:connect(onTouch)

1 answer

Log in to vote
1
Answered by
duckwit 1404 Moderation Voter
9 years ago

Your bullets aren't losing velocity, but the magnitude (speed) of the velocity is increasing due to gravitational acceleration. Your concern is not about the speed, though; it is about the direction, the path that a bullet takes.

Gravitational acceleration has a value of 196.2 studs per second^2 in ROBLOX, and to negate the effect of gravity all you need to do is add a BodyForce Instance that cancels out the weight of the bullet.

Add this to your onTouch() function:

--Add this after the line "bullet.BrickColor = ... ", after line 10
local antiGravity = Instance.new("BodyForce", bullet) --Create a new BodyForce and parent it to the bullet
antiGravity.force = Vector3.new(0, 196.2 * bullet:GetMass(), 0) --Give it a vertical force equal to the weight of the bullet

The calculation of the force uses Newton's Second Law of motion, that is: F = ma.

Force = Mass * Acceleration

We want a force that acts oppositely to the force of gravity. Gravity pulls down with an acceleration of 196.2 st/s^2, and the bullet has a specific mass. So we want a force that pulls up with an equal strength that gravity pulls down with, hence we multiply the mass of the bullet by the acceleration of gravity.

0
Thanks dude :D tommyax 7 — 9y
Ad

Answer this question