Creating an explosionForce method for 2D physics objects
For 3D games, the Rigidbody component has a useful method called AddExplosionForce(...)
that will cause an object to look as if it explodes from a given position. However, there is no such method for Rigidbody2D components. In this recipe, we’ll write an extension method for the Rigidbody2D
class so that we can add an explosionForce
method for 2D objects.
Thanks to Swamy for posting the code for the Rigidbody2D
extension class’s explosion force on Unity Forum (Nov. 21, 2013), which this recipe is based on:
Figure 8.60: Bugs being violently forced away from an invisible explosion point
Getting ready
This recipe builds on the previous one, so make a copy of that project and work on the copy for this recipe.
How to do it...
To create an explosionForce
method for 2D physics objects, follow these steps:
- Copy the project from the previous recipe, and use this copy for this recipe...