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 = Instance.new("Part")
cannonBall.Size = Vector3.new(1,1,1)
cannonBall.BrickColor = BrickColor.new(0) -- 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
		end
	end
	return nil
end

function fire(player)

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



	
	local missile = Instance.new("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 * Vector3.new(9, 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(Vector3.new(mag + 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 = Instance.new("ObjectValue")
	creator_tag.Value = player
	creator_tag.Name = "creator"
	creator_tag.Parent = missile
	


	missile.Parent = game.Workspace

end

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


                wait(0)     
                --debounce = false
		switch.BrickColor = BrickColor.new(37)
	--end
end

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

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 = Vector3.new(dx,dy,dz) + (mag * barrel.CFrame.LookVector)
1 Like

Thanks so much. That worked!!!

1 Like