Last active
September 9, 2017 01:37
-
-
Save johnsoncodehk/783295c6aa06593badfce037c810c7b2 to your computer and use it in GitHub Desktop.
[Unity] Pause Rigidbody Component
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
/* | |
* https://gist.github.com/johnsoncodehk/783295c6aa06593badfce037c810c7b2 | |
*/ | |
using UnityEngine; | |
[RequireComponent(typeof(Rigidbody))] | |
public class RigidbodyPause : MonoBehaviour { | |
[System.Serializable] | |
public struct OnPuaseData { | |
public Vector3 velocity; | |
public Vector3 angularVelocity; | |
} | |
public bool isPaused; | |
private bool m_IsPaused; | |
private Rigidbody m_Rigidbody; | |
private OnPuaseData m_OnPauseData; | |
void Start() { | |
this.m_Rigidbody = this.GetComponent<Rigidbody>(); | |
} | |
void Update() { | |
if (this.isPaused != this.m_IsPaused) { | |
if (this.isPaused) { | |
this.Pause(); | |
} | |
else { | |
this.Continue(); | |
} | |
this.m_IsPaused = this.isPaused; | |
} | |
} | |
private void Pause() { | |
this.m_OnPauseData.velocity = this.m_Rigidbody.velocity; | |
this.m_OnPauseData.angularVelocity = this.m_Rigidbody.angularVelocity; | |
this.m_Rigidbody.isKinematic = true; | |
} | |
private void Continue() { | |
this.m_Rigidbody.isKinematic = false; | |
this.m_Rigidbody.AddForce(this.m_OnPauseData.velocity, ForceMode.VelocityChange); | |
this.m_Rigidbody.AddTorque(this.m_OnPauseData.angularVelocity, ForceMode.VelocityChange); | |
} | |
} |
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
/* | |
* https://gist.github.com/johnsoncodehk/783295c6aa06593badfce037c810c7b2 | |
*/ | |
using UnityEngine; | |
[RequireComponent(typeof(Rigidbody2D))] | |
public class RigidbodyPauser2D : MonoBehaviour { | |
[System.Serializable] | |
public struct OnPuaseData { | |
public RigidbodyType2D bodyType; | |
public Vector2 velocity; | |
public float angularVelocity; | |
} | |
public bool isPaused; | |
private bool m_IsPaused; | |
private Rigidbody2D m_Rigidbody; | |
private OnPuaseData m_OnPauseData; | |
void Start() { | |
this.m_Rigidbody = this.GetComponent<Rigidbody2D>(); | |
} | |
void Update() { | |
if (this.isPaused != this.m_IsPaused) { | |
if (this.isPaused) { | |
this.Pause(); | |
} | |
else { | |
this.Continue(); | |
} | |
this.m_IsPaused = this.isPaused; | |
} | |
} | |
private void Pause() { | |
this.m_OnPauseData.bodyType = this.m_Rigidbody.bodyType; | |
this.m_OnPauseData.velocity = this.m_Rigidbody.velocity; | |
this.m_OnPauseData.angularVelocity = this.m_Rigidbody.angularVelocity; | |
this.m_Rigidbody.bodyType = RigidbodyType2D.Static; | |
} | |
private void Continue() { | |
this.m_Rigidbody.bodyType = this.m_OnPauseData.bodyType; | |
this.m_Rigidbody.velocity = this.m_OnPauseData.velocity; | |
this.m_Rigidbody.angularVelocity = this.m_OnPauseData.angularVelocity; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment