Ballistic Path Line Render

BallisticPathLineRender visualizes a projectile’s trajectory (including optional bounces) by drawing a line in world space. Designers can configure launch parameters, gravity behavior, collision layers, resolution, and bounce settings directly in the Inspector. Calling Simulate() (or enabling runOnStart/continuousRun) computes the full flight path via the corresponding BallisticsData.Predict(...) method and updates a LineRenderer to show the arc.
Public Fields & Properties
Start
public Vector3 startWorld-space origin point where the projectile is launched.
Designers can set this to a turret’s muzzle, character’s hand, or any desired source.
Projectile
public BallisticsData projectileReference to a
BallisticsDataasset or component that holds: initial speed, radius, drag, etc.Used internally to call
projectile.Predict(...)and retrieve aBallisticPath.
Run On Start
public bool runOnStart(default:true)If checked, the component immediately calls
Simulate()inStart(), rendering the path once when the scene begins.
Continous Run
public bool continuousRun(default:false)If enabled,
Simulate()is called everyLateUpdate(), allowing the line to update in real time as launch parameters change (e.g., movingstartor adjusting gravity).
Collision Layers
public LayerMask collisionLayers(default:0)Specifies which layers to test for collisions when predicting the path.
Designers must assign relevant layers (e.g., “Environment”, “Obstacles”) so that the trajectory stops or bounces correctly.
Trigger Interaction
public QueryTriggerInteraction triggerInteraction(default:UseGlobal)Controls whether triggers are included in collision checks (e.g.,
UseGlobal,Ignore,Collide).Adjust if you want the trajectory to stop or bounce off trigger colliders.
Resolution
[Min(0.001f)] public float resolution(default:0.1f)The distance increment at which the path is sampled.
Lower values (e.g.,
0.05f) yield a smoother, more accurate curve but cost more CPU.Higher values (e.g.,
0.2f–0.5f) produce fewer points and can miss short‐range collisions.
Max Length
public float maxLength(default:10f)Maximum distance the simulation traces before stopping if no impact occurs.
Designers can increase for long‐range arcs or decrease to limit preview length.
Max Bounces
public int maxBounces(default:0)Number of times the trajectory should bounce (up to) after its first impact.
Each bounce uses a reflected velocity scaled by
(1 – bounceDamping).
Bounce Damping
[Range(0f, 1f)] public float bounceDamping(default:0.2f)Fraction of velocity lost on each bounce (e.g.,
0.2fmeans the new speed is 80% of pre‐impact speed).Only used if
maxBounces > 0. Value must be in[0, 1].
Gravity Mode
public GravityMode gravityMode(enum:None,Physics,Custom; default:Physics)Selects how gravity is applied during simulation:
None– No gravity. The path is a straight line.Physics– UsesPhysics.gravity(typically(0, –9.81f, 0)).Custom– Uses the vector set incustomGravity.
Custom Gravity
public Vector3 customGravity(default:Vector3.zero)Only used if
gravityMode == GravityMode.Custom. Defines a custom gravity vector (e.g.,(0, –15f, 0)for heavier gravity).
Public Methods
Simulate
public void Simulate()Computes a full, sampled trajectory and updates the attached
LineRenderer.Designer-Visible Behavior:
Check “Speed”
If
projectile.Speed ≤ 0f,Simulate()returns immediately (nothing to draw).
Select Gravity
Uses
gravityModeto pickVector3 gravity = Physics.gravityorcustomGravity, orVector3.zero.
Clear Previous Data
Empties internal
List<Vector3> trajectoryandList<RaycastHit> impactsto avoid stale points.
Initial Prediction
csharpCopyEditvar result = projectile.Predict( start, // launch origin null, // no ignore-collider on first segment resolution, // sampling resolution maxLength, // maximum trace length collisionLayers, // layers to hit triggerInteraction, // include/exclude triggers gravity // chosen gravity vector );Appends every
step.positionfromresult.stepsintotrajectory.If
result.impact.HasValue, also stores theRaycastHitinimpacts.
Handle Bounces (if
maxBounces > 0and there was an impact)Computes
remainingLength = maxLength – result.flightDistance.Copies
projectilelocally (so original asset remains unchanged).Reflects last‐sample velocity about the impact normal (using
Vector3.Reflect) and multiplies by(1 – bounceDamping).Repeats
Predict(...)from the impact point, ignoring the now‐hit collider, up tomaxBouncestimes or until no further impact.Each subsequent segment’s positions are appended to
trajectory, and each new impact is added toimpacts.Deducts each segment’s
flightDistancefromremainingLengthto cap total drawn length.
Feed Data to LineRenderer
Ensures an internal buffer (
positionBuffer) is at leasttrajectory.Countin length; resizes if needed.Copies
trajectoryintopositionBuffer.Sets
lineRenderer.positionCount = trajectory.CountandlineRenderer.SetPositions(positionBuffer).
Designer Tips:
Call
Simulate()explicitly after changingstart,projectile, or any parameters at runtime (unlesscontinuousRunis true).Make sure
projectile.Speedis set properly in yourBallisticsDataasset before callingSimulate().If you want the line to auto-update whenever the camera or launch origin moves (e.g., an aiming reticle), enable
continuousRun = trueand adjust parameters in real time.
Inspector Setup & Typical Usage
Attach to a GameObject
Create an empty GameObject (e.g., “TrajectoryPreview”) in your scene.
Add a
LineRenderercomponent and configure its material (e.g., a simple unlit color) and width.Add the
BallisticPathLineRendercomponent to the same GameObject.
Assign Launch & Projectile Data
start: Drag a Transform (e.g., a turret’s muzzle or player’s hand) from your scene into this field or set a static coordinate.projectile: Select theBallisticsDataasset (ScriptableObject) or component that contains the projectile’s initial speed, radius, drag, etc.
Configure Collision & Bounce
collisionLayers: Tick the layers you want to hit (e.g., “Environment” or “Obstacles”).triggerInteraction: Choose whether triggers count as hits.maxLength: Set the maximum trace length in world units (e.g.,50f).maxBounces: If you want to preview up to N bounces, set this (e.g.,1for one bounce).bounceDamping: Adjust how much speed is lost each bounce (e.g.,0.3f→ 70% retention).resolution: Lower for finer curves (e.g.,0.05f), higher for performance (e.g.,0.2f).gravityMode:Physicsif you want standard Unity gravity.Customif you’re simulating in a non‐Earth environment (e.g.,customGravity = (0, –20f, 0)).Nonefor a straight-line preview (e.g., lasers or bullets without drop).
Choose Run Behavior
runOnStart=true→ Preview appears immediately when you enter Play Mode.continuousRun=true→ Preview updates every frame (useful for runtime adjusting aim).Otherwise, call
Simulate()from your script when you want to refresh the trajectory.
Scripting Examples
Manual Recalculation (e.g., when player aims):
public class TrajectoryController : MonoBehaviour { [SerializeField] private BallisticPathLineRender pathPreview; [SerializeField] private Transform muzzle; [SerializeField] private BallisticsData projectileData; void Update() { // Example: continuously update the start point and re-simulate pathPreview.start = muzzle.position; pathPreview.projectile = projectileData; pathPreview.Simulate(); } }One-Off Preview (e.g., on button press):
public class PreviewOnDemand : MonoBehaviour { [SerializeField] private BallisticPathLineRender pathPreview; [SerializeField] private Transform muzzle; [SerializeField] private BallisticsData projectileData; void Update() { if (Input.GetKeyDown(KeyCode.T)) { pathPreview.start = muzzle.position; pathPreview.projectile = projectileData; pathPreview.Simulate(); } } }
Designer Tips & Gotchas
LineRenderer Configuration
Adjust the material and width curve so the path is clearly visible (e.g., a bright color or dashed texture).
Performance Considerations
High
resolution(small steps) + many bounces can generate hundreds of sample points. If updating every frame (continuousRun), consider a coarserresolution(e.g.,0.2f–0.5f).If you only need a quick preview in the Editor and not at runtime, disable
continuousRunand runSimulate()via a custom Editor script or button.
Max Length vs. Bounces
maxLengthis total distance for each segment. Each bounce reducesremainingLengthby the previous segment’sflightDistance. If you want a guaranteed total limit across all bounces, subtract each segment from a master length before callingPredict(...).
Trigger Colliders
If you have invisible triggers (e.g., detection zones) in your scene, set
triggerInteraction = Ignoreif you don’t want the path to stop on them.
Ensuring Up-to-Date Data
If you change
BallisticsData(speed/radius) in Play Mode, callSimulate()again to reflect those changes. Otherwise, the path will still show the old values.
Last updated