Project
Space Shooter
Scoring, Finishing and Building the Game
Tutorial
Beginner
45 Mins
Unity Technologies
Overview
Summary
Add audio effects for the weapons, explosions and a background music track.
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.
Language
English
Recommended Unity Versions
5.1
1.
Audio
Add audio effects for the weapons, explosions and a background music track.

PlayerController

using UnityEngine; using System.Collections; [System.Serializable] public class Boundary { public float xMin, xMax, zMin, zMax; } public class PlayerController : MonoBehaviour { public float speed; public float tilt; public Boundary boundary; public GameObject shot; public Transform shotSpawn; public float fireRate; private float nextFire; void Update () { if (Input.GetButton("Fire1") && Time.time > nextFire) { nextFire = Time.time + fireRate; Instantiate(shot, shotSpawn.position, shotSpawn.rotation); audio.Play (); } } void FixedUpdate () { float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical); rigidbody.velocity = movement * speed; rigidbody.position = new Vector3 ( Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax) ); rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt); } }

js

class Boundary { var xMin : float; var xMax : float; var zMin : float; var zMax : float; } var speed : float; var tilt : float; var boundary : Boundary; var shot : GameObject; var shotSpawn : Transform; var fireRate : float; private var nextFire : float; function Update () { if (Input.GetButton("Fire1") && Time.time > nextFire) { nextFire = Time.time + fireRate; Instantiate(shot, shotSpawn.position, shotSpawn.rotation); audio.Play (); } } function FixedUpdate () { var moveHorizontal : float= Input.GetAxis ("Horizontal"); var moveVertical : float= Input.GetAxis ("Vertical"); var movement : Vector3= new Vector3 (moveHorizontal, 0.0f, moveVertical); rigidbody.velocity = movement * speed; rigidbody.position = new Vector3 ( Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax) ); rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt); }

boo

import UnityEngine import System.Collections [System.Serializable] public class Boundary: public xMin as single public xMax as single public zMin as single public zMax as single public class PlayerController(MonoBehaviour): public speed as single public tilt as single public boundary as Boundary public shot as GameObject public shotSpawn as Transform public fireRate as single private nextFire as single private def Update(): if Input.GetButton('Fire1') and (Time.time > nextFire): nextFire = (Time.time + fireRate) Instantiate(shot, shotSpawn.position, shotSpawn.rotation) audio.Play() private def FixedUpdate(): moveHorizontal as single = Input.GetAxis('Horizontal') moveVertical as single = Input.GetAxis('Vertical') movement = Vector3(moveHorizontal, 0.0F, moveVertical) rigidbody.velocity = (movement * speed) rigidbody.position = Vector3(Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax), 0.0F, Mathf.Clamp(rigidbody.position.z, boundary.zMin, boundary.zMax)) rigidbody.rotation = Quaternion.Euler(0.0F, 0.0F, (rigidbody.velocity.x * (-tilt)))

2.
Counting points and displaying the score
Counting points and displaying the score.

DestroyByContact

using UnityEngine; using System.Collections; public class DestroyByContact : MonoBehaviour { public GameObject explosion; public GameObject playerExplosion; public int scoreValue; private GameController gameController; void Start () { GameObject gameControllerObject = GameObject.FindWithTag ("GameController"); if (gameControllerObject != null) { gameController = gameControllerObject.GetComponent <GameController>(); } if (gameController == null) { Debug.Log ("Cannot find 'GameController' script"); } } 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.AddScore (scoreValue); Destroy(other.gameObject); Destroy(gameObject); } }

js

var explosion : GameObject; var playerExplosion : GameObject; var scoreValue : int; private var gameController : GameController; function Start () { var gameControllerObject : GameObject = GameObject.FindWithTag ("GameController"); if (gameControllerObject != null) { gameController = gameControllerObject.GetComponent (GameController); } if (gameController == null) { Debug.Log ("Cannot find 'GameController' script"); } } 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.AddScore (scoreValue); Destroy(other.gameObject); Destroy(gameObject); }

boo

import UnityEngine import System.Collections public class DestroyByContact(MonoBehaviour): public explosion as GameObject public playerExplosion as GameObject public scoreValue as int private gameController as GameController private def Start(): gameControllerObject as GameObject = GameObject.FindWithTag('GameController') if gameControllerObject is not null: gameController = gameControllerObject.GetComponent[of GameController]() if gameController is null: Debug.Log('Cannot find \'GameController\' script') 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.AddScore(scoreValue) Destroy(other.gameObject) Destroy(gameObject)

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; public GUIText scoreText; private int score; void Start () { score = 0; UpdateScore (); 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); } } public void AddScore (int newScoreValue) { score += newScoreValue; UpdateScore (); } void UpdateScore () { scoreText.text = "Score: " + score; } }

js

var hazard : GameObject; var spawnValues : Vector3; var hazardCount : int; var spawnWait : float; var startWait : float; var waveWait : float; var scoreText : GUIText; private var score : int; function Start () { score = 0; UpdateScore (); StartCoroutine (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); } } function AddScore (newScoreValue : int) { score += newScoreValue; UpdateScore (); } function UpdateScore () { scoreText.text = "Score: " + score; }

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 public scoreText as GUIText private score as int private def Start(): score = 0 UpdateScore() StartCoroutine(SpawnWaves()) private def Update(): if restart: if Input.GetKeyDown(KeyCode.R): Application.LoadLevel(Application.loadedLevel) 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) public def AddScore(newScoreValue as int): score += newScoreValue UpdateScore() private def UpdateScore(): scoreText.text = ('Score: ' + score)

3.
Ending the game
End the game and show the state of the game with labels for Game Over and Restart.

DestroyByContact

using UnityEngine; using System.Collections; public class DestroyByContact : MonoBehaviour { public GameObject explosion; public GameObject playerExplosion; public int scoreValue; private GameController gameController; void Start () { GameObject gameControllerObject = GameObject.FindWithTag ("GameController"); if (gameControllerObject != null) { gameController = gameControllerObject.GetComponent <GameController>(); } if (gameController == null) { Debug.Log ("Cannot find 'GameController' script"); } } 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 (); } gameController.AddScore (scoreValue); Destroy(other.gameObject); Destroy(gameObject); } }

js

var explosion : GameObject; var playerExplosion : GameObject; var scoreValue : int; private var gameController : GameController; function Start () { var gameControllerObject : GameObject = GameObject.FindWithTag ("GameController"); if (gameControllerObject != null) { gameController = gameControllerObject.GetComponent (GameController); } if (gameController == null) { Debug.Log ("Cannot find 'GameController' script"); } } 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 (); } gameController.AddScore (scoreValue); Destroy(other.gameObject); Destroy(gameObject); }

boo

import UnityEngine import System.Collections public class DestroyByContact(MonoBehaviour): public explosion as GameObject public playerExplosion as GameObject public scoreValue as int private gameController as GameController private def Start(): gameControllerObject as GameObject = GameObject.FindWithTag('GameController') if gameControllerObject is not null: gameController = gameControllerObject.GetComponent[of GameController]() if gameController is null: Debug.Log('Cannot find \'GameController\' script') 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() gameController.AddScore(scoreValue) Destroy(other.gameObject) Destroy(gameObject)

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; public GUIText scoreText; public GUIText restartText; public GUIText gameOverText; private bool gameOver; private bool restart; private int score; void Start () { gameOver = false; restart = false; restartText.text = ""; gameOverText.text = ""; score = 0; UpdateScore (); StartCoroutine (SpawnWaves ()); } void Update () { if (restart) { if (Input.GetKeyDown (KeyCode.R)) { Application.LoadLevel (Application.loadedLevel); } } } 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); if (gameOver) { restartText.text = "Press 'R' for Restart"; restart = true; break; } } } public void AddScore (int newScoreValue) { score += newScoreValue; UpdateScore (); } void UpdateScore () { scoreText.text = "Score: " + score; } public void GameOver () { gameOverText.text = "Game Over!"; gameOver = true; } }

js

var hazard : GameObject; var spawnValues : Vector3; var hazardCount : int; var spawnWait : float; var startWait : float; var waveWait : float; var scoreText : GUIText; var restartText : GUIText; var gameOverText : GUIText; private var gameOver : boolean; private var restart : boolean; private var score : int; function Start () { gameOver = false; restart = false; restartText.text = ""; gameOverText.text = ""; score = 0; UpdateScore (); StartCoroutine (SpawnWaves ()); } function Update () { if (restart) { if (Input.GetKeyDown (KeyCode.R)) { Application.LoadLevel (Application.loadedLevel); } } } 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); if (gameOver) { restartText.text = "Press 'R' for Restart"; restart = true; break; } } } function AddScore (newScoreValue : int) { score += newScoreValue; UpdateScore (); } function UpdateScore () { scoreText.text = "Score: " + score; } function GameOver () { gameOverText.text = "Game Over!"; gameOver = true; }

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 public scoreText as GUIText public restartText as GUIText public gameOverText as GUIText private gameOver as bool private restart as bool private score as int private def Start(): gameOver = false restart = false restartText.text = '' gameOverText.text = '' score = 0 UpdateScore() StartCoroutine(SpawnWaves()) private def Update(): if restart: if Input.GetKeyDown(KeyCode.R): Application.LoadLevel(Application.loadedLevel) 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) if gameOver: restartText.text = 'Press \'R\' for Restart' restart = true break public def AddScore(newScoreValue as int): score += newScoreValue UpdateScore() private def UpdateScore(): scoreText.text = ('Score: ' + score) public def GameOver(): gameOverText.text = 'Game Over!' gameOver = true

4.
Building the game
Build the completed game and deploy it to the web.
Please note that currently the Chrome browser has depreciated the NPAPI. This means many plugins, including Unity, will not work on Chrome. Select another compatible browser to use the Unity webplayer plugin.

Woohoo! You nailed this tutorial.
Continue rocking your Space Shooter project.
Next Step:
Extending Space Shooter
Tutorial
Beginner
210 Mins
Unity Technologies