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

Bullet only goes on Z axis, not Z axis relevant to the gun?

Asked by 9 years ago

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!!!

2 answers

Log in to vote
1
Answered by 9 years ago

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.

Ad
Log in to vote
2
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
9 years ago

If you want to use the relative z-axis of the handle, use script.Parent.Handle.CFrame.lookVector*100.

0
Sorry, didn't see your answer. Spongocardo 1991 — 9y

Answer this question