Day Three: Enemies and Explosions

by | Aug 20, 2020 | 30 Days of Gaming, GameDev | 0 comments

Thor I have no idea what I’m doing okay. I’m just trying to make a video game. So today we added in spawning enemies and destruction of the player object if it gets hit too many times.

Spawning Enemies

So to spawn in an enemy in unity we do the follow.

  1. Create an empty object.
  2. Assign an empty script to it.
  3. Create a function with IEnumerator this allows us to have infinite loops in order to spawn enemies every 5 seconds.

With an empty Spawn_Generator object here is the code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnManager : MonoBehaviour

	private GameObject _enemyPrefab;

	private GameObject _enemyContainer;

	private bool _stopSpawning = false;

    // Start is called before the first frame update
    void Start()

    // Update is called once per frame
    void Update()

	public void OnPlayerDeath() {
		_stopSpawning = true;

	//spawn game objects every 5 seconds.
	IEnumerator SpawnRoutine() {
		while ( ! _stopSpawning ) {
			Vector3 posToSpawn = new Vector3(Random.Range(-8f, 8f),7, 0);
			GameObject newEnemy = Instantiate( _enemyPrefab, posToSpawn, Quaternion.identity );
			newEnemy.transform.parent = _enemyContainer.transform;

			yield return new WaitForSeconds(5.0f);

Bonus: By creating an Enemy_Container object we can have new enemy object spawn under the Spawn_Manager instead of polluting the main Hierarchy.


Enemies and Laser both have rigid bodies with gravity destroyed. That way when they collide with another object that has a trigger we can respond to them hitting things.

For example an enemy when it hits something it destroys a laser and itself or damages the player model.

private void OnTriggerEnter(Collider other) {

		if (other.tag == "Player"){

			Player player = other.transform.GetComponent<Player>();

			if (player != null) {



		if (other.tag == "Laser") {

And when a Player model gets hit it subtracts one from it life. Once it hits zero lives it deleted itself.

	private int _lives = 3;

	public void Damage() {
		_lives -= 1;

		if(_lives < 1) {


I’m also starting to work on Pixel art for the game… but after spending the time that I did I realized I accidentally referenced the bottom of Battle Star Galactic for a top-down view. Lesson learned 30×30 is not big enough for a Battle Star let’s try 60×30.