Explosive Rocket Launcher in Unity

Sharing is caring:

Create Rockets that Explode Upon Impact

In this Unity tutorial we’ll go over how to create a simple rocket launcher that shoots out explosive rockets that explode on impact with other rigidbodies’ colliders. This can be used to shoot out any type of explosive projectile. We’ll start off by creating a simple “rocket launcher” cylinder, then apply an empty game object directly in front of the “barrel” of our rocket launcher.

This is where we will instantiate our rocket to shoot out of. Our rocket will have it’s own explosion script attached to it. The way this script will work is that once it collides into another collider then it will initiate the explosion. So we have our empty game object that instantiates and “shoots” out our rocket.

The rocket that is instantiated will then fly out and once it collides into an object (with a collider) it will explode on impact. If you shoot the rocket into the air it will not explode, so we’ll also need to include a “destroy” section to our script that it will destroy itself regardless after a set time period.

 

Javascript Files

RocketThrow.js (attach to your empty game object)


var rocket : Rigidbody;  //our prefab
var throwPower : float = 10; //power variable determines how fast this object will be "shot out"

function Update () {

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

var clone : Rigidbody; //variable within a variable

clone = Instantiate(rocket, 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

}

}

RocketScript.js (attach to your rocket prefab)


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

function Start () {

Destroy(gameObject, 3);

}

function OnCollisionEnter (collision : Collision) {

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

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

}

}

}

 

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. that is super cool.

  2. Hi great tutorial just one thing when I play the game the gun just shoots without me clicking and once it has I cant shoot again. Any help and I would be great full.

    • Jonathan Gonzalez says:

      Well one way this would happen is if you forgot the “if statement”. If you have everything in the if statement on it’s own it’ll automatically fire. Make sure you use the scripts provided and try it again.

Speak Your Mind

*