Using AddExplosionForce.
Objective: Using AddExplosionForce.
Before adding any code, we will start from our last article. Below is a link to the previous article.
https://medium.com/@suleimanabdullah/detection-of-collision-2480d3b1de99
In the first step, create two float variables: a radius and power.
Go inside the OnCollisionEnter, and when we check the tag, create the local position and an array collider. Then Use Physics.OverLapSphere, which takes position and radius as parameters.
Note: Physics.OverLapSphere is a function used to find all colliders that are within a given sphere’s radius, defined by a center point and radius. It returns an array of Collider objects
Now we need to loop through each collider, then get its component, but before doing anything, we need to check if the rigidbody is not null, then we call AddExplosionForce using RigidBody.
Note: AddExplosionForce takes four or 3 parameters: explosionForce, Position, radius,upwardModifier, and forceMode(Option, check the documentation below to learn more).
I have adjusted the power and radius
Bonus: To avoid the object from exploding again when it collides with the Hazard Tank, I have destroyed the gas tank after the loop.
Let's Play the Editor.
See you in the next one.