Using a choice of functions or simple macros, the path of any physics projectile can be predicted and displayed through a beam particle.
Using the initial location and velocity, the beam created by the blueprint will align itself to follow the path the projectile will take, the point it will land, and even where it will bounce.
This can be used to aim grenades, arrows, or nearly any physics-based projectile.
You can also use the function to predict the path of a projectile independently, without displaying it. Like a highly advanced Line/Sphere/Capsule/Box Trace that accounts for physics.
Three projects are which give examples of how to set up the assets inside of your blueprints, but I’ll give more details below.
This asset includes three basic functions, each with some variations based on your needs.
This is the function to spawn the emitters used to visualize the predicted path of your projectile. This should be run once when you begin aiming.
- Divisions: this controls the number of emitters and traces performed. Higher numbers are smoother and more accurate at the cost of performance.
- Beam size: this controls the width of the beam. A value of around 2 or higher will cause visible breaks in the beam.
- Color: this controls the color of the beam. Use values greater than 1 to add glow.
- Beam point texture: this sets the texture of the points along the beam.
- Marker texture: this sets the texture of the marker at the end of the beam.
This is the function used to calculate path projectile your projectile will take. This should be run each frame while aiming.
- Duration: this controls the length of travel in seconds that will be predicted for the projectile.
- Divisions: must be the same as in Spawn Trajectory Beam function
- Location: the point from which the projectile is spawned
- Velocity: the velocity applied to the projectile after spawning. Using Set Velocity is recommended. If you use Add Impulse, it will use the projectile’s mass, and you will need to divide the velocity by mass for an accurate result.
- Max bounces: the number of bounces that it will predict. Bounce prediction cannot be 100% accurate, and more bounces will be progressively less accurate. Additional bounces also require more resources to calculate and reduce performance.
- Restitution: this is the value of Restitution found in the projectile’s physical material. The default value without a physical material is 0.3.
- Friction: this is the value of Friction found in the projectile’s physical material. The default value without a physical material is 0.7.
- Gravity scale: this is the value of the projectile’s Gravity Scale setting, if you are using Projectile Movement component. The default value is 1.
- Object type: this is an array of surface types the trace will collide with. Use Make Array to give this a value.
- Trace complex: whether or not the trace should check against complex collision. Due to a bug in the engine, setting this to true will prevent bounces from being able to take the surface’s physical material into account, returning only default values.
- Actors to ignore: an array of actors in the scene that the trace will pass through and ignore. You must use Make Array to give this a value, but the array can be left empty.
- Draw debug type: ways in which to visualize the trace for debug purposes.
- Projectile movement: whether or not you are using a Projectile Movement component to give the projectile velocity. These calculate bounces differently from simply setting the velocity of an object.
And finally, this function is used to align the emitters with the predicted path. This should always be run immediately after calculating the path.
- Trajectory points: this should be connected to the output from Calculate Trajectory. It gives a list of points along the path on which to place the beam.
- Trajectory beam reference: this should be connected to the output from Spawn Trajectory Beam. It is a reference to the array of emitters spawned.
- Trajectory marker reference: this should be connected to the output from Spawn Trajectory Beam. It is a reference to the marker emitter spawned.
- Out hit: this should be connected to the output from Calculate Trajectory. It contains information on the final point of collision along the path, and is used to both place the marker, and align it with the surface.
Variations on each of these exist depending on how you want the path displayed, and which trace shape you want to use.
When you are done aiming, you will want to destroy the emitters created by the first function.
Three “Projectile Path Tracer” macros are also included. These combine the previous functions for ease-of-use, each with varying levels of complexity.
- Full: gives full access to all of the inputs from the included functions, and provides the most control. This also results in creating an extremely large and unwieldy node, but is much simpler than piecing the functions together yourself.
- Structured: uses a custom structure variable to cleanly contain most of the inputs, but this makes it difficult to change them at runtime.
- Simple: provides the fewest options, but is extremely small and easy use. This is intended mostly for prototyping, but is also useful if you have no need for the extra options.