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

How do I get this cannon to fire the way its facing?

Asked by 6 years ago
c = script.Parent.click
debounce = true
as = Workspace.as

script.Parent.click.ClickDetector.MouseClick:connect(function()
    if debounce == true then c.BrickColor = BrickColor.new("Really red")
        debounce = false
        pp = script.Parent.Barrel.Position
        b = Instance.new("Part", game.Workspace)
        fire = Instance.new("Fire", b)
        b.Name = "Canon Ball"
        b.BrickColor = BrickColor.new("Really black")
        b.Shape = "Ball"
        b.Size = Vector3.new(2, 2, 2)
        b.Position = Vector3.new(pp.X, pp.Y , pp.Z)



        b.Touched:connect(function(hit)
                if hit.Parent then if hit.Parent.Name == barrel then
                    print "h"
                end
                else
                    e = Instance.new("Explosion", b)
                    e.Position = b.Position
                    b:Destroy()
                    print(hit.Name)
                end
        end)


        wait(5)
        debounce = true
        c.BrickColor = BrickColor.new("Bright green")
    end
end)

Ive tried everything. All the body positions, CFrame and I cant get the cannon ball to fire in the direction the cannon is facing. I tried making the cannon ball point in the direction of the cannon but being confined to XYZ I have no idea how to do it. - Thanks

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago
local direction = Barrel.CFrame:vectorToWorldSpace(Vector3.new(0,0,-1))  -- gets direction from middle of part to the front face

b.Position = Barrel.Position + ((2 + Barrel.Size.Z/2) * direction)  -- sets position to in front of the barrel

b.Velocity = direction * 500
0
Thanks birdeater11 14 — 6y
0
Btw ill give you credit in my game if I ever finish it birdeater11 14 — 6y
Ad

Answer this question