How would I make bullets that are affected by gravity?

I’m making a cannon, and I want the cannonballs to fly in an arc as if it were being pulled down by gravity, rather than in a straight line.

For example, this is what it looks like now:

I’m using tweenservice to move the cannonballs. I’m doing it on the server but the network owner is set to the client. It doesn’t really seem to lag so I think I’m fine with this. Problem is that I don’t see how it’s possible to make the cannonballs be affected by gravity while using tweenservice, so I guess I’d have to use bodymovers instead. How would I go about doing that?

2 Likes

you either use Bezier Curves or this

4 Likes

Tried doing it with bezier curves, but the way I did it, it wasn’t too pretty:

Here’s what I did:

-- Got this function from the devhub...
function BezierFunction(t, p0, p1, p2)
	return (1 - t)^2 * p0 + 2 * (1 - t) * t * p1 + t^2 * p2
end

-- Unimportant what p0 p1 and p2 are
local p0 = Cannon.Cannon.CannonballPoint.WorldPosition
local p2 = Cannonball.Position + ((Cannon.Cannon.CFrame * CFrame.Angles(0, math.rad(-90), 0)).LookVector * -50)
local p1 = (p0 + p2) / 2
p2 -= Vector3.new(0, 10, 0)

-- The part where I actually tween the cannonball along the bezier path in 10 increments till it reaches its destination...
for i = 0.1, 1, 0.1 do
	local NextPos = BezierFunction(i, p0, p1, p2)
	Ts:Create(Cannonball, TweenInfo.new(0.1, Enum.EasingStyle.Linear), {Position = NextPos}):Play()
	wait(0.1)
end

It isn’t any prettier if done on the client.

Edit: Problem was just that the cannnonball was unanchored. looks good now:

Thanks 4 the advice :]

There are actually a couple of ways you can achieve this. One is to make an “artificial” gravity on the ball. The way it would work is to set a Vector force pushing the ball up and then add a downwards acceleration.

I don’t really recommend this as it is kind of already done by the physics system anyways. You could probably just use default Roblox physics for this.

Alternatively, you can use FastCast. There is a FastCast behaviour called Acceleration. If you set it so that it is downwards you can effectively make bullet drop. Plus the added raycast benefit.

Here is a handy tutorial I used when first using FastCast

2 Likes

I initially tried using fastcast but was a little bit overwhelmed. Didn’t know there was a video showing how to use it. If I have any problems with using tweenservice I’ll switch to fast cast. Thanks for that.