Skip to content

Instantly share code, notes, and snippets.

@lordcodes
Created December 13, 2016 20:56
Show Gist options
  • Save lordcodes/176df89431b67f84de467f1ee2e1c5fd to your computer and use it in GitHub Desktop.
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.
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