Cannon ball firing from side

So my cannon is acting strange and firing the cannon ball from the side of the barrel.

Does anyone know how to make the cannon ball fire from the front?

Here is my script:

local switch = script.Parent
local gunBarrelOne = script.Parent.Parent.GunBarrel.One
local gunBarrelTwo = script.Parent.Parent.GunBarrel.One

--local debounce = false
local gunOne = true

local cannonBall ="Part")
cannonBall.Size =,1,1)
cannonBall.BrickColor = -- gray
cannonBall.Shape = 0
cannonBall.BottomSurface = 0
cannonBall.TopSurface = 0
cannonBall.Name = "Cannon Shot"
cannonBall.Elasticity = .1
cannonBall.Reflectance = .2
cannonBall.Friction = .1

function getPlayer(humanoid)
	-- find the owning player of a humanoid.
	local players = game.Players:children()
	for i = 1, #players do
		if players[i].Character ~= nil then
			if players[i].Character.Humanoid == humanoid then return players[i] end
	return nil

function fire(player)

	local sound = script.Parent:findFirstChild("GunSound")
	if sound == nil then
		sound ="Sound")
		sound.Name = "GunSound"
		sound.SoundId = "rbxasset://sounds\\Rocket shot.wav"
		sound.Volume = 1
		sound.Parent = script.Parent

	local missile ="Part")

	local barrel

	barrel = gunBarrelOne

	if (barrel.Position - switch.Parent.Base.Position).magnitude > 16 then return end -- Blown up guns don't shoot. Usually.

	local spawnPos = barrel.Position *, 0.3, 0.8)

	local dx = math.random(-10,10)
	local dy = math.random(-10,10)
	local dz = math.random(-10,10)
	local mag = math.random(180,260)

	local v = barrel.CFrame:vectorToWorldSpace( + dx,dy,dz))
	local missile = cannonBall:clone()

	missile.Position = spawnPos
	missile.Velocity = v

	local new_script = script.Parent.CannonBall:clone()
	new_script.Disabled = false
	new_script.Parent = missile

	local creator_tag ="ObjectValue")
	creator_tag.Value = player
	creator_tag.Name = "creator"
	creator_tag.Parent = missile

	missile.Parent = game.Workspace


function onTouched(hit)
	--local humanoid = hit.Parent:findFirstChild("Humanoid")
	--if humanoid~=nil and debounce == false then
		--debounce = true
		switch.BrickColor =
		local player = hit

                --debounce = false
		switch.BrickColor =

while true do
	local player = script.Parent.Parent.Parent:WaitForChild("Player")
	local plr = game.Players:WaitForChild(player.Value)

It seems that instead of directing the missile to fire out of the front, you are just setting it to fly out mainly on the X axis.

Try setting the velocity to the LookVector(or a different directional Vector depending on which face of the cannon is the tip) of the cannon, plus the little bit of random velocity.

For example:

local v =,dy,dz) + (mag * barrel.CFrame.LookVector)
1 Like

Thanks so much. That worked!!!

1 Like