Explosions

Checked with version: 5.1

-

Difficulty: Beginner

Add explosions to the scene when hazards or the player is destroyed.

Explosions

Beginner Space Shooter tutorial

Please note that this project was developed for Unity version 4. It currently works, and has been checked, with Unity version 5.1. Please turn annotations on while watching the video, as we can add annotations to the videos when there are discrepancies between the recorded content and the current version of Unity.

Get the Upgrade Guide for Unity 5 here.

Please refer to the Official Q&A Page on the Forums for an Upgrade FAQ and to ask any questions.

DestroyByContact

Code snippet

using UnityEngine;
using System.Collections;

public class DestroyByContact : MonoBehaviour
{
    public GameObject explosion;
    public GameObject playerExplosion;

    void OnTriggerEnter(Collider other) 
    {
        if (other.tag == "Boundary")
        {
            return;
        }
        Instantiate(explosion, transform.position, transform.rotation);
        if (other.tag == "Player")
        {
            Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
            gameController.GameOver ();
        }
        Destroy(other.gameObject);
        Destroy(gameObject);
    }
}
var explosion : GameObject;
var playerExplosion : GameObject;

function OnTriggerEnter(other : Collider) 
{
    if (other.tag == "Boundary")
    {
        return;
    }
    Instantiate(explosion, transform.position, transform.rotation);
    if (other.tag == "Player")
    {
        Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
        gameController.GameOver ();
    }
    Destroy(other.gameObject);
    Destroy(gameObject);
}
import UnityEngine
import System.Collections

public class DestroyByContact(MonoBehaviour):
    public explosion as GameObject
    public playerExplosion as GameObject
    
    private def OnTriggerEnter(other as Collider):
        if other.tag == 'Boundary':
            return
        Instantiate(explosion, transform.position, transform.rotation)
        if other.tag == 'Player':
            Instantiate(playerExplosion, other.transform.position, other.transform.rotation)
            gameController.GameOver()
        Destroy(other.gameObject)
        Destroy(gameObject)

Related tutorials

Related documentation