Space Shooter

The goal of this game is to dodge the endless waves of meteors or destroy them to gain 10 points each, and score the highest score before your 3 lifes run out.
When 10 meteors have spawned it will wait 2 seconds bofore spawning 10 more.

My extra assignment for this project was to add a lifes machenic so that if you are hit by a meteor you will loose 1 life out of the 3 you start with, and if you run out of lifes a text would pop up saying ''Game Over'' and that you can press R to restart.

Spaceshooter ingame

Spaceshooter Unity

DestroyByBoundary.cs

using UnityEngine; using System.Collections; public class DestroyByBoundary : MonoBehaviour { void OnTriggerExit(Collider other) { Destroy(other.gameObject); } }
DestroyByContact.cs
using UnityEngine; using System.Collections; public class DestroyByContact : MonoBehaviour { public GameObject explosion; public GameObject playerExplosion; public int scoreValue; public int livesValue; private GameController gameController; void Start () { GameObject gameControllerObject = GameObject.FindWithTag("GameController"); if (gameControllerObject != null) { gameController = gameControllerObject.GetComponent(); } 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") { gameController.lifeLost(livesValue); Instantiate(playerExplosion, other.transform.position, other.transform.rotation); if (gameController.lives <= 0) { Destroy(gameObject); Destroy(other.gameObject); gameController.GameOver(); } } if (other.tag == "Bolt"){ Destroy(other.gameObject); } Destroy(gameObject); gameController.AddScore(scoreValue); } }
DestroyByTime.cs
using UnityEngine; using System.Collections; public class DestroyByTime : MonoBehaviour { public float lifetime; void Start () { Destroy(gameObject, lifetime); } }
GameController.cs
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; public GUIText livesText; private bool gameOver; private bool restart; private int score; public int lives; void Start () { gameOver = false; restart = false; restartText.text = ""; gameOverText.text = ""; score = 0; UpdateScore(); lives = 3; UpdateLives(); StartCoroutine (SpawnWaves()); } void Update () { if (restart) { if (Input.GetKeyDown (KeyCode.R)) { //Application.LoadLevel(Application.loadedLevel); Application.LoadLevel("Main"); } } } 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(); } public void lifeLost (int lifeMinus) { lives -= lifeMinus; UpdateLives(); } void UpdateScore () { scoreText.text = "Score: " + score; } void UpdateLives() { livesText.text = "Lives Remaining: " + lives; } public void GameOver () { gameOverText.text = "Game Over!"; gameOver = true; } }
Mover.cs using UnityEngine; using System.Collections; public class Mover : MonoBehaviour { public float speed; void Start () { GetComponent().velocity = transform.forward * speed; } }
PlayerController.cs 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; // GameObject clone = Instantiate(shot, shotSpawn.position, shotSpawn.rotation); // as GameObject; GetComponent().Play(); } } void FixedUpdate () { float moveHorizontal = Input.GetAxis ("Horizontal"); float moveVertical = Input.GetAxis ("Vertical"); Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical); GetComponent().velocity = movement * speed; GetComponent().position = new Vector3 ( Mathf.Clamp (GetComponent().position.x, boundary.xMin, boundary.xMax), 0.0f, Mathf.Clamp (GetComponent().position.z, boundary.zMin, boundary.zMax) ); GetComponent().rotation = Quaternion.Euler (0.0f, 0.0f, GetComponent().velocity.x * -tilt); } } RandomRotator.cs using UnityEngine; using System.Collections; public class RandomRotator : MonoBehaviour { public float tumble; void Start () { GetComponent ().angularVelocity = Random.insideUnitSphere * tumble; } }