Project
[Archived] Space Shooter
Boundaries, Hazards and Enemies
Tutorial
Beginner
50 Mins
Unity Technologies
Overview
Summary
This next tutorial in the Space Shooter series covers adding game screen boundaries, creating asteroid "hazards" for the ship, implementing a game controller, and setting up enemy spawning.
Language
English
Recommended Unity Versions
5.1
1.
Boundary
Creating a bounding box to destroy any object that leaves the game area.
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.

DestroyByBoundary

using UnityEngine; using System.Collections; public class DestroyByBoundary : MonoBehaviour { void OnTriggerExit(Collider other) { Destroy(other.gameObject); } }

js

#pragma strict function OnTriggerExit(other : Collider) { Destroy(other.gameObject); }

boo

import UnityEngine import System.Collections public class DestroyByBoundary(MonoBehaviour): private def OnTriggerExit(other as Collider): Destroy(other.gameObject)

2.
Creating hazards
Create an asteroid hazard to challenge the player.
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.

RandomRotator

using UnityEngine; using System.Collections; public class RandomRotator : MonoBehaviour { public float tumble; void Start () { rigidbody.angularVelocity = Random.insideUnitSphere * tumble; } }

js

var tumble : float; function Start () : void { rigidbody.angularVelocity = Random.insideUnitSphere * tumble; }

boo

import UnityEngine import System.Collections public class RandomRotator(MonoBehaviour): public tumble as single private def Start(): rigidbody.angularVelocity = (Random.insideUnitSphere * tumble)

DestroyByContact

using UnityEngine; using System.Collections; public class DestroyByContact : MonoBehaviour { void OnTriggerEnter(Collider other) { if (other.tag == "Boundary") { return; } Destroy(other.gameObject); Destroy(gameObject); } }

js

function OnTriggerEnter(other : Collider) { if (other.tag == "Boundary") { return; } Destroy(other.gameObject); Destroy(gameObject); }

boo

import UnityEngine import System.Collections public class DestroyByContact(MonoBehaviour): private def OnTriggerEnter(other as Collider): if other.tag == 'Boundary': return Destroy(other.gameObject) Destroy(gameObject)

3.
Explosions
Add explosions to the scene when hazards or the player is destroyed.
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

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); } }

js

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); }

boo

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)

4.
Game Controller
Write a controller to spawn our hazards, keep track of and display our score, end the game and allow the game to be restarted.
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.

GameController

using UnityEngine; using System.Collections; public class GameController : MonoBehaviour { public GameObject hazard; public Vector3 spawnValues; void Start () { SpawnWaves (); } void SpawnWaves () { Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z); Quaternion spawnRotation = Quaternion.identity; Instantiate (hazard, spawnPosition, spawnRotation); } }

js

var hazard : GameObject; var spawnValues : Vector3; function Start () { SpawnWaves (); } function SpawnWaves () { var spawnPosition : Vector3= new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z); var spawnRotation : Quaternion= Quaternion.identity; Instantiate (hazard, spawnPosition, spawnRotation); }

boo

import UnityEngine import System.Collections public class GameController(MonoBehaviour): public hazard as GameObject public spawnValues as Vector3 private def Start(): SpawnWaves() private def Update(): if restart: if Input.GetKeyDown(KeyCode.R): Application.LoadLevel(Application.loadedLevel) private def SpawnWaves() as IEnumerator: spawnPosition = Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z) spawnRotation as Quaternion = Quaternion.identity Instantiate(hazard, spawnPosition, spawnRotation)

5.
Spawning waves
Spawn infinite waves of hazards to challenge our player.
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.

GameController

using UnityEngine; using System.Collections; public class GameController : MonoBehaviour { public GameObject hazard; public Vector3 spawnValues; public int hazardCount; public float spawnWait; public float startWait; public float waveWait; void Start () { StartCoroutine (SpawnWaves ()); } IEnumerator SpawnWaves () { yield return new WaitForSeconds (startWait); while (true) { for (int i = 0; i < hazardCount; i++) { Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z); Quaternion spawnRotation = Quaternion.identity; Instantiate (hazard, spawnPosition, spawnRotation); yield return new WaitForSeconds (spawnWait); } yield return new WaitForSeconds (waveWait); } } }

js

var hazard : GameObject; var spawnValues : Vector3; var hazardCount : int; var spawnWait : float; var startWait : float; var waveWait : float; function Start () { SpawnWaves (); } function SpawnWaves () { yield WaitForSeconds (startWait); while (true) { for ( var i : int= 0; i < hazardCount; i++) { var spawnPosition : Vector3= new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z); var spawnRotation : Quaternion= Quaternion.identity; Instantiate (hazard, spawnPosition, spawnRotation); yield WaitForSeconds (spawnWait); } yield WaitForSeconds (waveWait); } }

boo

import UnityEngine import System.Collections public class GameController(MonoBehaviour): public hazard as GameObject public spawnValues as Vector3 public hazardCount as int public spawnWait as single public startWait as single public waveWait as single private def Start(): StartCoroutine(SpawnWaves()) private def SpawnWaves() as IEnumerator: yield WaitForSeconds(startWait) while true: for i in range(0, hazardCount): spawnPosition = Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z) spawnRotation as Quaternion = Quaternion.identity Instantiate(hazard, spawnPosition, spawnRotation) yield WaitForSeconds(spawnWait) yield WaitForSeconds(waveWait)

DestroyByTime

using UnityEngine; using System.Collections; public class DestroyByTime : MonoBehaviour { public float lifetime; void Start () { Destroy (gameObject, lifetime); } }

js

var lifetime : float; function Start () { Destroy (gameObject, lifetime); }

boo

import UnityEngine import System.Collections public class DestroyByTime(MonoBehaviour): public lifetime as single private def Start(): Destroy(gameObject, lifetime)

Woohoo! You nailed this tutorial.
Continue rocking your [Archived] Space Shooter project.
Next Step:
Scoring, Finishing and Building the Game
Tutorial
Beginner
45 Mins
Unity Technologies