Trajectory Simulation

Author: Matt Mechtley

Description
There are many cases where it's useful to calculate the trajectory an object will follow -- for instance when firing a ball from a cannon or the like. Perhaps you want to give the player some visual feedback about where their shot might land. The following script represents a very basic trajectory simulation algorithm that I used in the game Splume. It works best for simple spheres, and makes a number of assumptions:
 * The object being fired has 0 drag
 * Any object it bounces off has 0 friction and has friction combine set to Minimum
 * Any object it bounces off has 1 bouncyness and has bouncyness combine set to Maximum

That is, the object is assumed not to be losing any energy from drag or collisions. Simulating such cases is possible, but slightly more complicated.

Usage
It could potentially be costly to use this script in confined areas using many MeshColliders -- it raycasts 20 times per FixedUpdate. Also, the larger the collider attached to the fired ball, the more inaccurate this simulation becomes (since the actual ball hits with a point on its edge and not its center). This could be improved by using Physics.OverlapSphere instead of Physics.Raycast.

C# Version
Author: Benoit FOULETIER

Basically just regex-translated the above, with a few minor changes (hitObject should reference the Collider itself IMO, not the GameObject). More stuff could be cleaned, for example the nextColor thing is very Splume-specific. It's unclear what fireStrength is: if it's a force, then mass should be taken into account (even disregarding drag, friction and bouncyness)... trajectory prediction is a complex business!!

=PlayerFire.cs= This is the minimum you need to use the above. Attach it to an object pointing in an interesting direction (up is no fun). Strength has to be high enough otherwise all you'll see is a line pointing down.