100 Days of VR: Day 3 Going through the Unity Space Shooter Tutorial II

  • Creating a boundary box to delete objects
  • Creating enemies/obstacles

Boundaries, Hazard, and Enemies

Boundary

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

Creating Hazards

  • Used the provided asteroid model to create the GameObject
  • Attached a capsule collider component to it
  • Adjusted the collider to much the asteroid shape as much as possible
  • Added a Rigidbody component and made it a trigger
  • Added the provided RandomRotator script to the asteroid
using UnityEngine;
using System.Collections;
public class RandomRotator : MonoBehaviour
{
public float tumble;
void Start ()
{
GetComponent<RigidBody>().angularVelocity = Random.insideUnitSphere * tumble;
}
}

AngularVelocity

Destroy Asteroids When Shot

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

Explosions

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);
}
Destroy(other.gameObject);
Destroy(gameObject);
}
}

Re-using Code

Game Controller

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);
}
}
public GameObject hazard;
public Vector3 spawnValues;
void SpawnWaves ()
{
Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);
}

Spawning waves

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 &amp;lt; 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);
}
}
}

Coroutine

yield return new WaitForSeconds (spawnWait);
void Start()
{
StartCoroutine(test());
print("end start");
}
IEnumerator test()
{
for (int i = 0; i < 3; i++)
{
print("in for loop " + i);
yield return new WaitForSeconds(1);
}
}
void Update()
{
StartCoroutine(test());
}
IEnumerator test()
{
for (int i = 0; i < 3; i++)
{
print("in for loop " + i);
yield return new WaitForSeconds(1);
}
}

Cleaning up Explosions

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

Conclusion

  • How to use boundaries to clean up some of our GameObject leaving it
  • How to create, move, and destroy enemy waves.
  • How to use corroutines, which in some ways are similar to Update()

--

--

--

Software Engineer by day, side hustler wannabee by night! https://leetdev.io

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Detecting CSS Animation and Transition Events

A basic introduction to functional programming principles in ReasonML

React Native & Mac

Back to some JavaScript Basics, Part II

How to Integrate Card Payment with Stripe into Your Web App without Back-end

9 JavaScript Projects You Can Build to Perfect Your Coding Skills

My GSOC Journey #3: The Great Rework

Customize your windows terminal with Oh My Posh

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Josh Chang

Josh Chang

Software Engineer by day, side hustler wannabee by night! https://leetdev.io

More from Medium

My journey becoming a Unity game developer: Make a game look beautiful-Putting up the walls to make…

Getting started with GAME DEVELOPMENT.

Game Development: Beginners Prospective

WGTU #5: Versatility VS specialisation — with Hearthstone