Created
December 13, 2016 20:56
-
-
Save lordcodes/176df89431b67f84de467f1ee2e1c5fd to your computer and use it in GitHub Desktop.
A Unity C# script to attach to a GameObject with a Rigidbody2D so that it can be paused and resumed.
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 class PausableRigidBody2D : MonoBehaviour { | |
private Rigidbody2D body; | |
private Vector3 savedVelocity; | |
private float savedAngularVelocity; | |
void Awake() { | |
body = GetComponent<Rigidbody2D>(); | |
} | |
public void Pause() { | |
savedVelocity = body.velocity; | |
savedAngularVelocity = body.angularVelocity; | |
body.isKinematic = true; | |
} | |
public void Resume() { | |
body.isKinematic = false; | |
body.velocity = savedVelocity; | |
body.angularVelocity = savedAngularVelocity; | |
body.WakeUp(); | |
} | |
public void Reactivate() { | |
body.isKinematic = false; | |
body.WakeUp(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment