When I click, the bullet doesn't go straight. Instead, it comes out different ways. I don't know why it does this. Help?
01 | local tool = script.Parent |
02 | local reloading = script.Parent.Reloading |
03 | local autofire = false |
04 |
05 | function fireBullet(d) |
06 | local bullet = Instance.new( "Part" ) |
07 | bullet.FormFactor = "Custom" |
08 | bullet.Size = Vector 3. new( 1.5 , 0.35 , 0.35 ) |
09 | bullet.BrickColor = BrickColor.new( "White" ) |
10 | bullet.Position = tool.Handle.Position + (d * 5 ) |
11 | bullet.Velocity = d * 125 |
12 |
13 | local s = script.RemoveBullet:Clone() |
14 | s.Disabled = false |
15 | s.Parent = bullet |
Try changing your force to this.
1 | force.force = Vector 3. new( 0 , 100 , 0 ) -- 100 is perfect. its a mostly straight shot |
If that doesn't work then put it like this
1 | force.force = Vector 3. new( 100 , 0 , 0 ) |
Or this
1 | force.force = Vector 3. new( 0 , 0 , 100 ) |
The mass of every object in ROBLOX is measured by it's volume. In order to get an object's volume, you have to multiple the Width by the Length by the Height.
1 | local BrickMass = Brick.Size.X * Brick.Size.Y * Brick.Size.Z |
This is for any brick.
Now that number alone only gives you mass of the object. Now we have to find out what the exact weight of that object is.
In physics, you learn that the weight of an object is simply M * g
, which is the Mass
of the object multiplied by the gravitational acceleration
in that environment. On Earth, the gravitation acceleration is 9.81 m/s^2
. This means that an object in free fall will accelerate at 9.81 m/s^2 downward. The gravitational acceleration in ROBLOX, however, is 196.2 studs/second^2
. So to get the weight of the object, take the mass of the object and multiply it by the ROBLOX gravitational acceleration.
1 | local BrickWeight = BrickMass * 196.2 |
Now that you have the weight of the brick, you need to put that in the force. Like this:
1 | force.force = Vector 3. new( 0 , BrickWeight, 0 ) |
And there you go! That'll keep your bullet in a straight line path all the time!
FULL FUNCTION:
01 | function fireBullet(d) |
02 | local bullet = Instance.new( "Part" ) |
03 | bullet.FormFactor = "Custom" |
04 | bullet.Size = Vector 3. new( 1.5 , 0.35 , 0.35 ) |
05 | bullet.BrickColor = BrickColor.new( "White" ) |
06 | bullet.Position = tool.Handle.Position + (d * 5 ) |
07 | bullet.Velocity = d * 125 |
08 |
09 | local s = script.RemoveBullet:Clone() |
10 | s.Disabled = false |
11 | s.Parent = bullet |
12 |
13 | local BulletWeight = Bullet.Size.X * Bullet.Size.Y * Bullet.Size.Z * 196.2 --This gets the force required to keep the bullet hovering |
14 | local force = Instance.new( "BodyForce" ) |
15 | force.force = Vector 3. new( 0 , BulletWeight, 0 ) |
16 | force.Parent = bullet |
17 |
18 | bullet.Parent = game.Workspace |
19 | end |
Look, Do like this:
01 | function fireBullet(d) |
02 |
03 | local bullet = Instance.new( "Part" ) |
04 |
05 | bullet.FormFactor = "Custom" |
06 |
07 | bullet.Size = Vector 3. new( 1.5 , 0.35 , 0.35 ) |
08 |
09 | bullet.BrickColor = BrickColor.new( "White" ) |
10 |
11 | bullet.CFrame = CFrame.new(script.Parent.Handle.Position,d) |
12 |
13 | bulletForce = Instance.new( "BodyVelocity" , bullet) |
14 |
15 | bulletForce.maxForce = Vector 3. new( math.huge , math.huge , math.huge ) |
That'd fix the bullet flying in all directions possible error thing. Aight?