# PredictProjectile - Calculate projectile motions easily!

Have you ever wanted to make some kind of indicator to tell where your bullets would lead? Have you ever wanted to make your projectile-based game more easier for noobs as seen below?

Fear not! I have a solution for you!

# PredictProjectile

PredictProjectile is a module that is very simple to use, and its API is relatively easy to understand.
Here’s the RBXM:
PredictProjectile.rbxm (1.6 KB)

Here’s an example of using it:

The use of the module is relatively self-explanatory. If you want to make the 3D Vector position into a 2D UI, check out Camera:WorldToScreenPoint

## Notes

When calculating anything with this, please put the actual origin and not just the root of a model. For example, if you use a firingPoint attachment, use the WorldPosition of that for the calculations. Not doing so can result in inaccurate results
Best used with FastCast

## API

``````:FindLeadShot(targetPosition, targetVelocity, projectileSpeed, shooterPosition, shooterVelocity, gravity)
``````

`targetVelocity` is the velocity your target is moving at (in Vector3)
`gravity` is a POSITIVE number of your gravity (if your workspace’s gravity is -197.2, then input 197.2)
`shooterVelocity` is the velocity your shooter is moving at (in Vector3). If your projectile firing system doesn’t inherit the shooter’s velocity, you can just set this as `Vector3.new(0,0,0)`.

Returns a `Vector3` where the projectile would arrive at, given the projectile’s speed.

``````:FindMaximumRange(speed, gravity, initialHeight)
``````

Finds the maximum ballistic range, accounting for gravity.
Returns: Studs, or numbers

``````:FindAngleToShootAt(origin, projectileSpeed, targetPosition, gravity)
``````

Finds the angle (in degrees) at which something should be fired at.
Returns:
lowerAngle, higherAngle

## Conclusion

Any problems? Criticism? Let me know in the replies.

44 Likes

Great!, but this would work In no-fps games?, like for projectiles, spheres, balls

Well, really depends on the calculations done for the projectile. If it measures roughly in studs per second, then it’ll work for you

You don’t have to use fastcast or anything, just input the velocity (as long as if it is studs / s)

Hi, do you think you could post the .rbxm of this? Roblox is down atm so I can’t download it.

Thanks.

1 Like

Hm, looks like I can’t even open studio. It keeps throwing an app error.

1 Like

It not for sale ,you maybe want to fix it…

Oops! Sorry.

I’m opening the module as a .rbxm file, so you should be able to get it there

1 Like

# New update!

• Simplified the functions, now there’s only 1 : )
• Improved calculation methods
1 Like

# Minor update

• Fixed gravity time calculation issues. The module should now actually compensate for gravity (exponential gravity)
• Showcase of the prediction module (paired with FastCast and TurretController modules): PDC will now compensate for gravity

I think you forgot to put the module on sale again. And the linked .rbxm file isn’t updated either since I don’t see anything that involves gravity.
Other than that, pretty cool and useful module!

WHY DO I KEEP DOING THAT

I’m terribly sorry, I’ll put it up for sale and update the .rbxm

2 Likes

New update!

• Added `:FindMaximumRange` to find the maximum range at which a projectile can be fired, accounting gravity.
• Added `:FindAngleToShootAt` to alternatively find the angle at which to fire a projectile.
• Changed `:GetPredictedPosition` to `:FindLeadShot`
1 Like

I found a forum with the same code you written in :FindLeadShot
do you by any chance, take the source from here? Projectile shot calculations - #21 by Den_vers

2 possibilities

1: He used the same reference code I used when converting it into lua
2: He used my code

I took the :FindLeadshot code from some Unity game page, and converted it into Luau form, so possibility 1 seems more likely

interesting, would you mind specify this unity page?

I’m terribly sorry but I don’t have it. I’ve been trying to find it for a very long time. However, I do know the name of the website has something to do with the keyword of gamedev and unity

Hi new update

Fixed a minor issue where the gravity argument wouldn’t affect the outcome

Do you have a demo place? At lower bullet speeds, it seems to have a habit of missing the target for some reason.
Edit- AlignPosition doesn’t apply a velocity! I think that was my issue

Is there an example for predicting with the high angle?

1 Like

I don’t have an example at the moment, apologies.

1 Like

If you figure one out, please post it. I love the module!