/// <summary> /// Draws a <seealso cref="Rigidbody2D" />'s trajectory using <seealso cref="Debug" />.DrawLine. /// </summary> /// <param name="rb"> The body whose trajectory is being drawn. </param> /// <param name="force"> For predicting the effects of a Rigidbody2D.AddForce() method. Use Vector2.zero if not needed. </param> /// <param name="mode"> Determines how the force vector changes the velocity. Irrelevant when using Vector2.zero. </param> /// <param name="color"> The color of the line being drawn. </param> /// <param name="trajectoryDuration"> Amount of time in seconds to predict. </param> /// <param name="lineDuration"> Amount of time in seconds the drawn line will persist. </param> /// <param name="constantForce"> Will the force be applied every FixedUpdate. </param> /// <param name='depthTest'> Whether or not the line should be faded when behind other objects. </param> public static void DebugTrajectory(this Rigidbody2D rb, Vector2 force, ForceMode2D mode, Color color, float trajectoryDuration = 1.0f, float lineDuration = 0.0f, bool constantForce = false, bool depthTest = false) { var positions = rb.GetTrajectory(force, mode, trajectoryDuration, constantForce); for (var i = 0; i < positions.Length - 1; i++) { Debug.DrawLine(positions[i], positions[i + 1], color, lineDuration, depthTest); } }
/// <summary> /// Draws a <seealso cref="Rigidbody2D" />'s trajectory using <seealso cref="Gizmos" />.DrawLine. /// </summary> /// <param name="rb"> The body whose trajectory is being drawn. </param> /// <param name="force"> For predicting the effects of a Rigidbody2D.AddForce() method. Use Vector2.zero if not needed. </param> /// <param name="mode"> Determines how the force vector changes the velocity. Irrelevant when using Vector2.zero. </param> /// <param name="color"> The color of the line being drawn. </param> /// <param name="trajectoryDuration"> Amount of time in seconds to predict. </param> /// <param name="constantForce"> Will the force be applied every FixedUpdate. </param> public static void DrawTrajectory(this Rigidbody2D rb, Vector2 force, ForceMode2D mode, Color color, float trajectoryDuration = 1.0f, bool constantForce = false) { var oldColor = Gizmos.color; Gizmos.color = color; var positions = rb.GetTrajectory(force, mode, trajectoryDuration, constantForce); for (var i = 0; i < positions.Length - 1; i++) { Gizmos.DrawLine(positions[i], positions[i + 1]); } Gizmos.color = oldColor; }
/// <summary> /// Calculates a <seealso cref="Rigidbody2D" />'s trajectory and returns it as an array of position vectors. /// </summary> /// <param name="rb"> The body whose trajectory is being drawn. </param> /// <param name="force"> For predicting the effects of a Rigidbody2D.AddForce() method. Use Vector2.zero if not needed. </param> /// <param name="mode"> Determines how the force vector changes the velocity. Irrelevant when using Vector2.zero. </param> /// <param name="trajectoryDuration"> Amount of time in seconds to predict. </param> /// <param name="constantForce"> Will the force be applied every FixedUpdate. </param> /// <returns> /// An array of the body's expected positions after each FixedUpdate in the trajectory. The last index being the /// final position. /// </returns> public static Vector2[] GetTrajectory(this Rigidbody2D rb, Vector2 force, ForceMode2D mode, float trajectoryDuration = 1.0f, bool constantForce = false) { Vector2[] velocities; return(rb.GetTrajectory(force, mode, out velocities, trajectoryDuration, constantForce)); }