Learn Technologies
Unity Throw Grenade / Bomb

GRENADE / BOMB in Unity – Tutorial part 4

Hello guys, in this new tutorial of Unity. Today, you’ll learn how to Throw Grenade / Bomb and make some explosion effects using free assets in Unity store.

You can see also my video tutorial on YouTube step by step:

Free assets

First of all, we need to import 2 package from assets store:

Adding code

Let’s create 2 classes called GrenadeThrower.cs and Grenade.cs

The first class will define the behavior of throwing Grenade and the second one will define the explosion effect.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GrenadeThrower : MonoBehaviour
{
    public GameObject GrenadePref;
    public GameObject FirePoint;
    public int GrenadeNumber = 99;
    public float throwForce = 300;

    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void ThrowGrenade()
    {
        if(GrenadeNumber > 0)
        {
            GrenadeNumber--;
            GameObject grenadeObject = Instantiate(GrenadePref, FirePoint.transform.position, FirePoint.transform.rotation);
            Rigidbody grenadeRigidbody = grenadeObject.GetComponent<Rigidbody>();
            grenadeRigidbody.AddRelativeForce(FirePoint.transform.forward * throwForce, ForceMode.Impulse);
        }
    }
}

In Grenade class, create a private method called Explode(), in which we instantiate the explosion effect. Also, we look for the touched enemies by this explosion by defining the explosion radius and for each touched enemy we call TakeDamage method.
Finally we destory the Grenade game object.

using System.Linq;
using UnityEngine;

public class Grenade : MonoBehaviour
{
    private float countDown;
    private bool hasExplosed;

    public GameObject ExplosionEffect;
    public float Delay = 3f;
    public float GrenadeRadius = 5000f;
    public float ExplosionForce = 7000f;
    public float DamageRate = 60f;

    void Start()
    {
        countDown = Delay;

    }

    // Update is called once per frame
    void Update()
    {
        countDown -= Time.deltaTime;
        if (countDown <= 0f && !hasExplosed)
        {
            Explode();
            hasExplosed = true;
        }
    }

    private void Explode()
    {
        Instantiate(ExplosionEffect, transform.position, transform.rotation);
        Collider[] touchedObjects = Physics.OverlapSphere(transform.position, GrenadeRadius).Where(x => x.tag == "Enemy").ToArray();

        foreach (Collider touchedObject in touchedObjects)
        {
            Rigidbody rigidbody = touchedObject.GetComponent<Rigidbody>();
            if(rigidbody != null)
            {
                rigidbody.AddExplosionForce(ExplosionForce, transform.position, GrenadeRadius);
            }

            var target = touchedObject.gameObject.GetComponent<EnemyMovment>();
            target.TakeDamage(DamageRate);

        }
        Destroy(gameObject);
    }
}

Follow Me For Updates

Subscribe to my YouTube channel or follow me on Twitter or GitHub to be notified when I post new content.

5 1 vote
Article Rating
Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
waterfall

Pretty! This was an extremely wonderful article.

Domenica Wanzer

It’s actually a great and useful piece of info. I’m happy that you simply shared this helpful info with us. Please keep us up to date like this. Thanks for sharing.|

blackstar2015

how did you get line 35 and 45? you seemed to have magically gotten that code to work in your project. even in the video it appeared out of no where. but when i try to type it in im getting errors.

3
0
Would love your thoughts, please comment.x
()
x