Last active
August 29, 2015 13:57
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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