Problem with cannonball physics

Description


I'm currently working on scripting a cannon. Now i want to make a cannonball shoot out of the cannon when pressing F. To register the player presses 'F' I'm using ContextActionService and this event fires the server. This works so far.

My Problem


I used this script to make the cannon shoot:
local canFire = true

FireCannon.OnServerEvent:Connect(function()
	if canFire then
		canFire = false

		wait(.25)		
		
		local newBall = CannonBall:Clone()
		newBall.CFrame = CannonBarrel.CFrame
		newBall.Velocity = CannonBarrel.CFrame.lookVector * 50 -- Speed 50
		newBall.Parent = workspace
		newBall.Anchored = false
    	        wait(1)
		canFire = true
	end
end)

However the cannonball is falling out of the side. How can make the cannonball flying out where it should fly out?

Help appreciated.

1 Like

So your going to want to unanchor the cannonball before you parent it to the workspace. I’d go ahead and just make CannonBall.Anchored = false wherever you created it. Also you need to make the CannonBall spawn outside of barrel or turn the collisions of the barrel off. (Usually I just weld an invisible, collision less part the end of the barrel and use that as a “FirePoint” where the ball should spawn.

1 Like

I changed lookVector to RightVector and followed your instructions and now it works sort of. The problem is that it looks like the cannonball stops for a short time mid-air and the travels further. This occurs directly after it is launched.

Try changing this

FireCannon.OnServerEvent:Connect(function(Player)
-- other code...
local newBall = CannonBall:Clone()
newBall:SetNetworkOwner(Player);

Doing something of that nature gives client control over said Cannonball. What I mean by this, is if they caught on then it might get abused. Personally, I think you should use something along the lines of fast casting, and then just making a client sided visual effect of the cannonball and tween it to the hitpos. Making a combat game with ranged weapons? FastCast may be the module for you!