Last active
July 7, 2018 19:26
-
-
Save EliCDavis/e3b049383a3eb40b6e4a6e2e32df54b3 to your computer and use it in GitHub Desktop.
a code sample for https://medium.com/scry-llc/applying-explosion-knock-back-1f394f7160e8
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
float desiredMagnitudeToEndForceAnimation = .1f; | |
IEnumerator AnimateForce(float force, Vector3 forcePosition, float radius) | |
{ | |
var rb = GetComponent<Rigidbody>(); | |
rb.constraints = RigidbodyConstraints.None; | |
rb.AddExplosionForce(force, forcePosition, radius); | |
do | |
{ | |
yield return new WaitForSeconds(.1f); | |
} | |
while (rb.velocity.magnitude > desiredMagnitudeToEndForceAnimation && rb.angularVelocity.magnitude > desiredMagnitudeToEndForceAnimation); | |
// It could have been destroyed at any point in time in our waiting | |
if(rb != null) | |
{ | |
rb.constraints = RigidbodyConstraints.FreezeAll; | |
} | |
} | |
public void AddExplosionForce(float force, Vector3 forcePosition, float radius) | |
{ | |
StartCoroutine(AnimateForce(force, forcePosition, radius)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment