Skip to content

Instantly share code, notes, and snippets.

@bbuck
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bbuck/9717162 to your computer and use it in GitHub Desktop.
Save bbuck/9717162 to your computer and use it in GitHub Desktop.
Physics2D polyfill to support ForceMode (Unity 3D). Source: http://answers.unity3d.com/questions/574864/rigidbody2d-and-forcemodeaddvelocity.html
using UnityEngine;
public static class Physics2DExtensions {
public static void AddForce(this Rigidbody2D rigidbody2D, Vector2 force, ForceMode mode = ForceMode.Force) {
switch (mode) {
case ForceMode.Force:
rigidbody2D.AddForce(force);
break;
case ForceMode.Impulse:
rigidbody2D.AddForce(force / Time.fixedDeltaTime);
break;
case ForceMode.Acceleration:
rigidbody2D.AddForce(force * rigidbody2D.mass);
break;
case ForceMode.VelocityChange:
rigidbody2D.AddForce(force * rigidbody2D.mass / Time.fixedDeltaTime);
break;
}
}
public static void AddForce(this Rigidbody2D rigidbody2D, float x, float y, ForceMode mode = ForceMode.Force) {
rigidbody2D.AddForce(new Vector2(x, y), mode);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment