Grenade Launcher in Unity3d

Sharing is caring:

What you’ll learn

In this Xenosmash tutorial we’ll show you how to create a grenade launcher in Unity3d. This grenade launcher will shoot out grenades that explode  and add force to our rigidbody boxes in our scene. We’ll be making some changes to our default grenade from a previous video by including an explosion sound clip and a particle system to make things look much nicer. We also applied a simple bronze like texture to our grenade just to distinguish it from our grenade launcher. The grenade launcher has an empty game object attached to it that will instantiate our grenades. This script was also modified heavily to include plenty of GUI information to show how much ammo we have and to update automatically once we shoot and reload. We’ll also include a reload function that will add and subtract ammo from our total ammo count. Sound clips will play for our shooting and reloading function as well. Links to sound clips below.

Sound Clips

http://soundbible.com/1467-Grenade-Explosion.html http://soundfxnow.com/sound-fx/thumper-m-79-launcher/

Javascript Files

 

ExplosionScript.js


var radius : float = 5.0; //provides a radius at which the explosive will effect rigidbodies
var power : float = 10.0; //provides explosive power
var explosiveLift : float = 1.0; //determines how the explosion reacts. A higher value means rigidbodies will fly upward
var explosiveDelay : float = 5.0; //adds a delay in seconds to our explosive object
var explosionPrefab : Transform; //Put your particle system prefab in this slot
var explosionSound : AudioClip; //Audio clip for the explosion

function Update ()

{

Fire();

}

function Fire () {

yield WaitForSeconds(explosiveDelay); //provides a delay for our explosion to enable our grenades to be shot out

Instantiate(explosionPrefab, transform.position, transform.rotation); //instantiates our particle system prefab to play when the grenade explodes

var grenadeOrigin : Vector3 = transform.position;

var colliders : Collider[] = Physics.OverlapSphere (grenadeOrigin, radius); //this is saying that if any collider within the radius of our object will feel the explosion

for(var hit : Collider in colliders){  //for loop that says if we hit any colliders, then do the following below

if (hit.rigidbody){

hit.rigidbody.AddExplosionForce(power, grenadeOrigin, radius, explosiveLift); //if we hit any rigidbodies then add force based off our power, the position of the explosion object

AudioSource.PlayClipAtPoint(explosionSound, transform.position, 1); //plays our explosion sound clip

Destroy(gameObject); //the radius and finally the explosive lift. Afterwards destroy the game object.

}

}

}

GrenadeThrow.js


var grenade : Rigidbody;  //our prefab
var throwPower : float = 10; //power variable determines how fast this object will be "shot out"
var shootGrenade : AudioClip; //sound clip to shoot our grenade launcher
var reloadGrenade : AudioClip; //sound clip to reload our grenade launcher
var ammoCount : int = 6; //how much ammo in our clip
var totalAmmo : int = 12; //how much total ammo we have available
var reloadTime : int = 3; //how long it takes before we've reloaded
var reloadAmount : int = 6; //how many grenades we'll reload
var ammo : GUIText; //our GUI text that displays our ammo in text format
var fireRate : float = 0.5; //how quickly we can shoot our grenades
private var nextFire : float =0.0;

function Update () {

Fire ();
Reload();
OnGUI();

}

function Fire () {

if (ammoCount > 0) {

if (Input.GetButtonDown("Fire1") && Time.time > nextFire) {

nextFire = Time.time + fireRate;

InstantiateGrenade();

ammoCount -=1;

}

}

}

//---------------------------------------------------------------------------------

function InstantiateGrenade () {

if (Input.GetButtonDown("Fire1")) {

var clone : Rigidbody; //variable within a variable

clone = Instantiate(grenade, transform.position, transform.rotation); //the clone variable holds our instantiate action

clone.velocity = transform.TransformDirection(Vector3.forward * throwPower); //applies force to our prefab using the "forward" position times our throwPower variable

AudioSource.PlayClipAtPoint(shootGrenade, transform.position, 1);

}

}

//---------------------------------------------------------------------------------

function Reload () {

if(ammoCount < 1 && totalAmmo > 5) { //if we have less than 1 bullet then we can reload

if(Input.GetKeyDown("r")) {

AudioSource.PlayClipAtPoint(reloadGrenade, transform.position, 1); //plays reload soundclip

yield WaitForSeconds(reloadTime); //waits for "reloadTime" before adding ammo

ammoCount += reloadAmount; //adds ammo to our "clip" based off the reloadAmount

totalAmmo -= reloadAmount; //subtracts whatever the reloadAmount was from our total ammo every time we reload

}

}

}

//---------------------------------------------------------------------------------

function OnGUI () {

ammo.text = "Ammo: " + ammoCount + "/" +  totalAmmo.ToString(); //displays our GUI Text in the format we want. The sections in "" will appear exactly as their written

}

 

Jonathan Gonzalez

I love all things in game development and want to contribute by teaching others how to create games. I'm always looking to help others create awesome digital worlds. I've been a hardcore gamer since I was a kid and now it's my turn to create awesome digital experiences.

More Posts - Website

Follow Me:
TwitterYouTube

Comments

  1. Awesome tutorial. Still a little confused though with rigidbodies vs transform objects –
    var clone : Rigidbody; //variable within a variable
    I can only get mine to play if I declare clone as a transform, otherwise I get “cannot convert Unityengine.Transform to UnityEngine.Rigidbody” ?

Speak Your Mind

*