Unity dev, during my summer break, I decided to refresh my programming knowledge using C# and Unity to create a simple space shooter game.

“Unity is a cross-platform game engine developed by Unity Technologies, first announced and released in June 2005 at Apple Inc.’s Worldwide Developers Conference as an OS X-exclusive game engine. As of 2018, the engine has been extended to support 27 platforms. The engine can be used to create both three-dimensional and two-dimensional games as well as simulations for desktops and laptops, home consoles, smart TVs, and mobile devices. Several major versions of Unity have been released since its launch, with the latest stable version being Unity 2018.2.2, released on August 10, 2018”

It’s been a long time since I wrote any code, however it didn’t take me long to get back into the swing of things, my simple space shooter provides the player with some power-ups, a running score and some cool FX making use of the Unity game developer package.

My Player code in C#

using System.Collections;
using UnityEngine;
public class Player : MonoBehaviour
{
public bool canTripleshot = false;
public bool SpeedboostActive = false;
public bool ShieldsActive = false;
public int playerLives = 3;
[SerializeField]
private GameObject _explotionPrefab;
[SerializeField]
private GameObject _laserPrefab;
[SerializeField]
private GameObject _tripleShotPrefab;
[SerializeField]
private GameObject _shieldgameObject;
[SerializeField]
private GameObject _thrusterGameObject;
[SerializeField]
private GameObject[] _engines;
[SerializeField]
private float _fireRate = 0.25f;
private float _canFire = 0.0f;
[SerializeField]
private float _speed = 5.0f;
private UIManager _uiManager;
private GameManager _gameManager;
private SpawnManager _spawnManager;
private AudioSource _audioSource;
private int _hitCount;
// Use this for initialization
void Start()
{
transform.position = new Vector3(0, 0, 0);
_uiManager = GameObject.Find("Canvas").GetComponent<UIManager>();
if (_uiManager != null)
{
_uiManager.UpdateLives(playerLives);
}
_gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
_spawnManager = GameObject.Find("SpawnManager").GetComponent<SpawnManager>();
if (_spawnManager != null)
{
_spawnManager.StartSpawnRoutine();
}
_audioSource = GetComponent<AudioSource>();
_hitCount = 0;
}
// Update is called once per frame
void Update()
{
Movement();
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
{
Shoot();
}
}
private void Shoot()
{
if (Time.time > _canFire)
{
_audioSource.Play();
if (canTripleshot == true)
{
Instantiate(_tripleShotPrefab, transform.position, Quaternion.identity);
}
else
{
Instantiate(_laserPrefab, transform.position + new Vector3(0, 0.88f, 0), Quaternion.identity);
}

_canFire = Time.time + _fireRate;
}
}
private void Movement()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
if (SpeedboostActive == true)
{
transform.Translate(Vector3.right * _speed * 1.5f * horizontalInput * Time.deltaTime);
transform.Translate(Vector3.up * _speed * 1.5f * verticalInput * Time.deltaTime);
//Debug.Log("SpeedBoost On");
}
else
{
transform.Translate(Vector3.right * _speed * horizontalInput * Time.deltaTime);
transform.Translate(Vector3.up * _speed * verticalInput * Time.deltaTime);
//Debug.Log("SpeedBoost Off");
}

if (transform.position.y > 0)
{
transform.position = new Vector3(transform.position.x, 0, 0);
}
else if (transform.position.y < -4.2f) { transform.position = new Vector3(transform.position.x, -4.2f, 0); } if (transform.position.x > 9.5f)
{
transform.position = new Vector3(-9.5f, transform.position.y, 0);
}
else if (transform.position.x < -9.5f)
{
transform.position = new Vector3(9.5f, transform.position.y, 0);
}
}
public void Damage()
{

if (ShieldsActive == true)
{
ShieldsActive = false;
_shieldgameObject.SetActive(false);
return;
}
_hitCount++;
if (_hitCount == 1)
{
_engines[0].SetActive(true);
}
else if (_hitCount == 2)
{
_engines[1].SetActive(true);
}
playerLives--;
_uiManager.UpdateLives(playerLives);
if (playerLives < 1)
{
Instantiate(_explotionPrefab, transform.position, Quaternion.identity);
_gameManager.gameOver = true;
_uiManager.ShowTitleScreen();
Destroy(this.gameObject);
}
}
public void TripleshotPowerupOn()
{
canTripleshot = true;
StartCoroutine(TripleShotPowerDownRoutine());
}
public IEnumerator TripleShotPowerDownRoutine()
{
yield return new WaitForSeconds(5.0f);
canTripleshot = false;
}
public void SpeedBoostOn()
{
SpeedboostActive = true;
StartCoroutine(SpeedBoostDownRoutine());
}
public IEnumerator SpeedBoostDownRoutine()
{
yield return new WaitForSeconds(9.0f);
// Debug.Log("SpeedBoost off");
SpeedboostActive = false;
}
public void EnableShields()
{
ShieldsActive = true;
_shieldgameObject.SetActive(true);
}

}

My Enemy AI in C#

using UnityEngine;

public class EnemyAI : MonoBehaviour
{
[SerializeField]
private GameObject _enemyExplotionPrefab;
private float _speed = 5.0f;

private UIManager _uiManager;
[SerializeField]
private AudioClip _clip;
// Use this for initialization
void Start()
{
_uiManager = GameObject.Find("Canvas").GetComponent<UIManager>();

}

// Update is called once per frame
void Update()
{
transform.Translate(Vector3.down * _speed * Time.deltaTime);
if (transform.position.y < -7)
{
float randomX = Random.Range(-7, 7);
transform.position = new Vector3(randomX, 7, 0);
}

}

private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Laser")
{
if (other.transform.parent != null)
{
Destroy(other.transform.parent.gameObject);
}
Destroy(other.gameObject);
Instantiate(_enemyExplotionPrefab, transform.position, Quaternion.identity);
_uiManager.UpdateScore();
AudioSource.PlayClipAtPoint(_clip, Camera.main.transform.position, 1f);
Destroy(this.gameObject);
}
else if (other.tag == "Player")
{
Player player = other.GetComponent<Player>();

if (player != null)
{
player.Damage();
}
Instantiate(_enemyExplotionPrefab, transform.position, Quaternion.identity);
AudioSource.PlayClipAtPoint(_clip, Camera.main.transform.position, 1f);
Destroy(this.gameObject);

}
}

}

My Spawn system in C#

using System.Collections;
using UnityEngine;

public class SpawnManager : MonoBehaviour
{
[SerializeField]
private GameObject enemyShipPrefab;
[SerializeField]
private GameObject[] powerups;
private GameManager _gameManager;

// Use this for initialization
void Start()
{
_gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
StartCoroutine(EnemySpawnRoutine());
StartCoroutine(PowerupSpawnRoutine());
}

public void StartSpawnRoutine()
{
StartCoroutine(EnemySpawnRoutine());
StartCoroutine(PowerupSpawnRoutine());
}
// create a coroutine to spawn enemy every 5 secs

IEnumerator EnemySpawnRoutine()
{
while (_gameManager.gameOver == false)
{
Instantiate(enemyShipPrefab, new Vector3(Random.Range(-7f, 7f), 7, 0), Quaternion.identity);
yield return new WaitForSeconds(5.0f);
}
}

IEnumerator PowerupSpawnRoutine()
{
while (_gameManager.gameOver == false)
{
int randomPowerup = Random.Range(0, 3);
Instantiate(powerups[randomPowerup], new Vector3(Random.Range(-7, 7), 7, 0), Quaternion.identity);
yield return new WaitForSeconds(5.0f);

}
}
}

I was surprised by how much I remembered from coding in my Uni days and I am a firm believer that you never stop learning especially when it comes to computers.

If you would like to try the game out here are the links:

Techie Mike
Techie Mike
Self-taught techie, with a passion for computers and all the cool things you can do with them. Techie Mike, B.Eng. B.Sc.
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to Techie Mike - The IT guy in Thailand.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.