Projectile Toolkit > Targeting and Prediction Version 3.1
Targeting and Prediction describes the static library Projectile. It contains static methods to solve targeting (aka ballistics) problems and to predict various projectile data.
Targeting and Prediction API provides various algorithms to meet the needs of different scenarios, here are some example use cases:
| Method | Example use case |
|---|---|
| VelocityByA | This method automatically adapts max height of the trajectory according to the distance to the target. Great for human-like throwing/jumping behavior, and projectile launch calculation in 3D top-down shooters. |
| VelocityByAngle | Launch projectiles to hit a target with a specific elevation angle. |
| VelocityByTime | Let archers to accurately hit moving targets. |
| VelocityByHeight | Use in animations, or achieve realistic NavMesh Link movement, or achieve jump pad mechanism. |
| AnglesBySpeed | Simulate weapons that has a specific launch speed, such as cannon and mortar. |
| VelocitiesBySpeed | An extended version of AnglesBySpeed. It is more convenient than AnglesBySpeed when the rotation is not separated into y axis and x axis, such as hand-held mortar and bow. |
| ElevationalReach | Display the weapon’s maximum attack range. |
| Method | Example use case |
|---|---|
| PositionAtTime | Implement anti-ballistic missile (use this method to predict the position of the hostile projectile after x seconds, and use VelocityByTime(..., x) to launch the anti-ballistic missile). |
| Positions | Predict the trajectory of a projectile. (You can use TrajectoryPredictor component instead, it has trajectory rendering implemented.) |
| VerticalFlightTest | Predict the flight time of a projectile when the x and z coordinates of the target are unkown, but the elevation of the target is known. |
| FlightTest | Test if a certain velocity will allow a projectile to hit the target. |
Important
Add using Blobcreate.ProjectileToolkit; in your scripts to be able to call the APIs.
To launch a projectile to hit a target:
Add a Collider and a Rigidbody to your prefab if it doesn't have one (you can set Interpolate to Interpolate and set Collision Detection to Continuous Dynamic to get the best result),
In your code, instantiate the prefab,
Call one of the targeting algorithms to calculate the launch velocity, and apply it using AddForce(...).
The targeting algorithms are all static methods so the integration is very flexible.
Example code
Take VelocityByTime(...) for example, if you want AI units to accurately striking moving objects:
xxxxxxxxxx[SerializeField] Rigidbody projectilePrefab;[SerializeField] Transform launchPoint;[SerializeField] float timeOfFlight;...// In your own method:var myRigid = Instantiate(projectilePrefab, launchPoint, launchPoint.rotation);var v = Projectile.VelocityByTime(myRigid.position, predictedPos, timeOfFlight);myRigid.AddForce(v, ForceMode.VelocityChange);View Defender.cs for the whole implementation of this (how to calculate predictedPos, etc.).
Additionally, you can add a SimpleExplosive component to your prefab, it handles collision events, explosion VFX, explosion force, and damage. Add the following logic below the above lines to make it work properly (for the above example, targetPosition is predictedPos):
xxxxxxxxxxmyRigid.GetComponent<ProjectileBehaviour>().Launch(targetPosition);For 2D
In 2D, everything is the same, except that Rigidbody2D does not have ForceMode2D.VelocityChange mode, so we use ForceMode2D.Impulse instead:
xxxxxxxxxxmyRigid2D.AddForce(myRigid2D.mass * v, ForceMode2D.Impulse);
There are 3 approaches to do trajectory prediction, here is a table that compares their pros and cons:
| Projectile.Positions(...) | TrajectoryPredictor | PEBTrajectoryPredictor | |
|---|---|---|---|
| Description | Static method that returns an array of Vector3 representing the points of the trajectory. | Component that handles calculation and rendering of trajectory. | Component that handles calculation and rendering of trajectory. The calculation uses the physics engine. |
| Calculation | Algorithmic; Non-iterative1 | Algorithmic; Non-iterative1 | Physics-engine based; Iterative |
| Collisions and Bounces | No | No (collision is planned) | Yes, accurate bounces |
| Predict Aerodynamics | No | No | Yes |
| Performance | High | High | Medium |
| Usage | Link | See below | Link |
Important
⚠️ If you encounter that the trajectory is not showing, search the following materials in the Project panel and click on them one by one, and the problem should be fixed: "Dash Line", "Slash Line", "SquareParticle". (This is an Editor bug in older Unity versions.)
How to use TrajectoryPredictor
Drag and drop the prefab "Trajectory Predictor.prefab" in folder "Blobcreate/Projectile Toolkit/Prefabs" into your scene,
In your script, add the following logic:
xxxxxxxxxx[SerializeField] TrajectoryPredictor tp;...// Update() or your own methodvoid Update(){ // Call Render to update the positions of the line. tp.Render(launchPosition, launchVelocity, distanceOrEnd);}Or if you want to predict the trajectory of a moving rigidbody:
xxxxxxxxxxtp.Render(myRigid.transform.position, myRigid.velocity, distanceOrEnd);Note
"distanceOrEnd" here means that you can either put "distance (float)", or "end (Vector3)" end point of the trajectory (if known), to control the length of the predicted trajectory line.
Click here to play the online version (WebGL).
You can explore the demos under the folder "Blobcreate/Projectile Toolkit/Demos".
Some setup is required:
1. Materials
The materials are URP (Render Pipeline) materials. Make sure to test the scenes in a URP project.
Important
⚠️ If you encounter that the trajectory is not showing, search the following materials in the Project panel and click on them one by one, and the problem should be fixed: "Dash Line", "Slash Line", "SquareParticle". (This is an Editor bug in older Unity versions.)
2. Visual setup (optional)
If your project uses URP but there are rendering problems with demo scenes you can use "PTK-URP-HighQuality" render pipeline asset.
3. Set up layers and physics
Tip
These settings are for the demo scenes, they are not required for your own scenes/projects. Backups are important if your project already has custom layers.
Back up your layer settings and physics settings, and apply the layer preset "PTKLayers" and physics preset "PTKPhysics" under ".../Demos/Other Assets/Settings". Detailed steps:
(At the top right of Unity editor) select "Layers > Edit Layers...",
Click the second icon in the top right of the inspector,
Click "Save current to..." button to back up your current layer settings,
Click that second icon again and then choose "PTKLayers" in the pop up window.
In Unity 6 and above, in physics settings, the layer collision matrix should be manually set to the same as below. Also, set the Gravity to (0, -29.43, 0).

Which scene demonstrates which algorithm? The relation is shown in the table below:
| Method | Scene Index(es) / class(es) |
|---|---|
| VelocityByA | 02 / JumpAttacker ProjectileLauncher |
| VelocityByAngle | 03 / CannonLike |
| VelocityByTime | 02 / Defender |
| VelocityByHeight | 00 / JumpTester, 01 / NMJump, 02 / JumpAttacker |
| AnglesBySpeed | 03 / CannonLike |
| VelocitiesBySpeed | 03 / CannonLike |
| PositionAtTime | Demo coming soon |
| Positions | Used in TrajectoryPredictor component. |
| ElevationalReach | 03 / CannonLike |
| VerticalFlightTest | Used in FlightTest(...) method. |
| FlightTest | 03 / CannonLike |
For TrajectoryPredictor component, the demo scene is 02. For other features, you can find the corresponding demo scenes by their names.
Tip
The script filenames of the classes are class + .cs. You can find them under the folder "Blobcreate/Projectile Toolkit/Demos/Scripts".