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

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

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

✓ Accepted answer
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

2 comments

0

Thanks

0

Btw ill give you credit in my game if I ever finish it

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