I made a script, when you click, it shoots a bullet. Unfortunately, the bullet only goes on the Z axis, not the Z axis relevant to the gun! Why is that happening? Here is my script
wait(1) script.Parent.Equipped:connect(function(mouse) mouse.Button1Down:connect(function() -- local animation = script.Slash -- local plyr = script.Parent.Parent.Name -- local char = workspace:FindFirstChild(plyr) -- local anim = char.Humanoid:LoadAnimation(animation) -- anim:Play() local bullet = Instance.new("Part") bullet.FormFactor = "Custom" bullet.Size = Vector3.new(0.5,0.2,0.2) bullet.BrickColor = BrickColor.new("Toothpaste") bullet.BackSurface = "SmoothNoOutlines" bullet.BottomSurface = "SmoothNoOutlines" bullet.FrontSurface = "SmoothNoOutlines" bullet.LeftSurface = "SmoothNoOutlines" bullet.RightSurface = "SmoothNoOutlines" bullet.TopSurface = "SmoothNoOutlines" bullet.Velocity = Vector3.new(0,0,100) bullet.Parent = workspace bullet.Position = script.Parent.Handle.Position bullet.Transparency = 0 bullet.CanCollide = true --local speed = script.Speed:clone() --speed.Parent = bullet local damage = script.DamageScript:clone() damage.Parent = bullet print("HALLAAA") end) end)
Please help!!! Thank you!!!
For the velocity, you're just firing the bullet off the Z axis. You can set the velocity to the lookVector of the gun's handle multiplied by the speed of the bullet.
lookVector * Speed
We can get the lookVector of the gun's handle by using it's CFrame. The following code can replace the bullet's velocity line so that the bullet travels out of the gun at a speed of 100 studs.
bullet.Velocity = script.Parent.Handle.CFrame.lookVector * 100
I hope my answer helped you. If it did, be sure to accept it.
If you want to use the relative z-axis of the handle, use script.Parent.Handle.CFrame.lookVector*100
.