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.