BallisticRaycaster API (overview)

Convenience overload used by DemoGun: #

Parameters (key ones): #

  • start: Muzzle position.
  • dir: Shot direction (normalized internally).
  • muzzleVelocity: Initial speed.
  • gravity: Gravity vector.
  • maxTime, dt: Simulation time horizon and step size.
  • maxRange: Max travel distance (DemoGun uses Infinity; time and dt control length).
  • layers, triggerInteraction: Physics filters.
  • selfRoot: Transform to ignore (weapon/rig).
  • out RaycastHit hit: First valid hit.
  • out float timeToHit: Time-of-flight to impact.
  • List outPoints: Optional path points for tracers (filled by the call).
  • int maxPoints: Cap on points pushed to outPoints.
  • int tempHitCapacity: Internal non-alloc hit buffer capacity.

Usage Patterns #

Ballistics vs Straight Cast #

Ballistics On:

  • Simulates gravity-curved flight using Max Time and Time Step.
  • Tracer.InitializePath is called with the simulated points and timeToHit.

Ballistics Off:

  • Performs a straight Physics.Raycast using max range ≈ Muzzle Velocity × Max Time for fair comparisons.
  • Tracer.Initialize is called with a straight segment from muzzle to hit (or forward to a default length on miss).

Aiming at Screen Center #

  • Enable Use Camera to cast from the camera through the viewport center.
  • The muzzle direction is computed from Firepoint to the aim point (closest valid hit or a far
  • fallback).
  • Face Screen Center can rotate your weapon to match the current aim direction.
  • Pooling tracers
  • Assign a Tracer prefab and enable Auto Pool Tracers.
  • Warmup pre-instantiates instances; the pool grows up to Max Size (or further if Can Grow).
  • Tracers clear themselves after their lifetime. The DemoGun returns them to the pool with a small padding time.

Spawning impacts and decals #

DemoGun logs hit info when a collision occurs. To add effects:

  • Use the returned RaycastHit in FireBallistic or FireStraight to spawn decals, particles, sounds.
  • For ballistic, timeToHit gives accurate time-of-flight for timed effects or lag compensation.

Tuning and Best Practices #

  • Time Step (dt): 0.01–0.02 is a good starting point. Lower dt improves accuracy of fast/short shots; higher dt reduces CPU cost.
  • Max Time: Upper bound on simulated time-of-flight (limits arc length and points).
  • Points: maxPoints should be ceil(MaxTime / dt) + a small safety margin. DemoGun clamps to 2–512.
  • Ignore self: Always assign Self Root so the shot doesn’t collide with your own rig.
  • Rendering quality:
    · Keep End Skin slightly > 0 to avoid “through surface” visuals when viewed from the side.
    · Consider 0 cap vertices (flat caps) for LineRenderer to reduce overhang.
  • Pooling:
    · Prefer prefab + pooling for frequent shots.
    · Keep Tracer.Destroy = false to reuse instances efficiently.

What are your feelings

Updated on November 7, 2025