Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is this gun script not shooting straight when I turn my helicopter's guns?

local mouse = game.Players.LocalPlayer:GetMouse()

mouse.KeyDown:connect(function(key)
	if key == "f" then
		Barrel = script.Parent.Parent.Parent.Character.Experimentalheli.Controls.Barrel
		Killscript = script.Script
		c = Instance.new("Part")
		c.Parent = game.Workspace
		c.Name = "Bullet"
		c.BrickColor = BrickColor.new("Bright yellow")
		c.Size = Vector3.new(1, 1, 4)
		c.BottomSurface = "Smooth"
		c.TopSurface = "Smooth"
		c.Position = Barrel.Position
		c.CanCollide = false
		b = Instance.new("BlockMesh")
		b.Scale = Vector3.new(0.3, 0.3, 1)
		b.Parent = c
		t = Instance.new("BodyThrust")
		t.force = Vector3.new(0, -2000, -130000)
		t.Parent = c
		a = Killscript:clone()
		a.Parent = c
		Barrel.Firesound:Play()
	end
end)

I'm new at scripting but yet I managed to make a functional helicopter

1 answer

✓ Accepted answer

Well first of all, the force of the BodyThrust is always the same magnitude and the same direction, no matter where you're facing, so that's why. I would recommend using a BodyVelocity instead of a BodyForce because a bodyforce will just cause the bullet to keep accelerating instead of going at constant speed, whereas bodyvelocity keeps it at a constant velocity.

To get the direction of how you want the bullet to travel, we use something called a lookVector. A lookVector is basically a vector3 with a magnitude of 1 that points in the direction that a cframe is facing. To fix your script, we replace this:

t = Instance.new("BodyThrust")
t.force = Vector3.new(0, -2000, -130000)
t.Parent = c

With this:

t = Instance.new("BodyVelocity")
t.maxForce = Vector3.new(math.huge, math.huge, math.huge) --This gives the bodyvelocity infinite power, which prevents it from being affected by the bullet's weight
t.velocity = Barrel.CFrame.lookVector * 100 --This makes the velocity of the brick the direction that the barrel is facing. The "* 100" is gives the vector a magnitude of 100, which means that the speed of the bullet will be 100 studs per second in whatever direction the barrel is facing
t.Parent = c

Hope this helped!

1 comment

0

Thanks! Also I love your gun kit infact im using it on my FPS game

This discussion is preserved as it was. New answers, comments, and votes are closed.