Creating an Explosion Effect in Unity🙌
- santosh nalla
- Oct 1, 2019
- 2 min read
Updated: Mar 13, 2020
"Grenades". Yes, who doesn't use them in their games especially if you creating a first/third person shooter games. Creating the explosion effect is rather simple, with a very fundamental math involved. Fine, Lets get started.
For this all you need is a Grenade Model( or you can use a basic sphere as your Grenade) and explosion Particle effect. For explosion particle effect, you have lots of free assets in unity store itself or you can make one for yourself( I am not gonna tell how to do this here).
Take this Grenade, Add Rigid body and box/Sphere Collider to this. Create a C# script and add the script to our grenade.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Explosion : MonoBehaviour
{
public Collider[] hitColliders; // To get all colliders inside the overlapSphere.
public float pintime; //Time aafter which the grenade should explode
public float graniteRadiusEffect; // Effect radious of the grenade
public GameObject ExplosionEffectParticles; // Grenade pxplosion particle Effect.
void Start()
{
pintime = 6.0f; //set pin time to 6 seconds
graniteRadiusEffect = 20.0f;
Invoke("ExplosionEffect", pintime); // Invoke ExplosionEffect function where the grenade explodes after 6 seconds
}
void ExplosionEffect()
{
cam.shakeDuration = 0.3f;
Instantiate(ExplosionEffectParticles, transform.position, Quaternion.identity); //Instantiate explosion particle effect.
gameObject.SetActive(false); // Once explosion happends(in last line) visually grenade should disappear.
hitColliders = Physics.OverlapSphere(transform.position, graniteRadiusEffect); // You draw a sphere around grenade and see what objects are comming under that sphere(to add force)
foreach (var item in hitColliders)
{
Vector3 distance = transform.position - item.transform.position; // You calculate the distance of our grenade and object in our sphere. Based on the distance, we apply force on the object, we divide force value by distance because force is inversely proportional to distance.
if (!(item.transform.gameObject.tag == "Plane")) // you ignore ground
{
item.GetComponent<Rigidbody>().AddForce((-transform.position + item.transform.position).normalized * (500.0f / distance.magnitude)); // Here we calculate what direction the object is present to apply force in that direction and we divide by distance as distance is inversely proportional to force applies
item.GetComponent<Rigidbody>().AddForce(item.transform.up * (500.0f / distance.magnitude));
}
}
}
void OnDrawGizmos() // Draw Gizmo for our visual view.
{
Gizmos.DrawWireSphere(transform.position, 2.0f);
}
}

So, hitColliders = Physics.OverlapSphere(transform.position, graniteRadiusEffect), creates a sphere around the grenade and stores all the game objects which are available in the graniteRadiusEffect. We take this objects, we calculate the distance( Vector3 distance = transform.position - item.transform.position;) and direction(-transform.position + item.transform.position), there objects are available in the grenade sphere, and apply force based on the distance( note: distance is inversely proportional to the force applied by grenade).
item.GetComponent<Rigidbody>().AddForce((-transform.position + item.transform.position).normalized * (500.0f / distance.magnitude));
This is the final result. To make it feel more alive i have added a character. 😜
Comments