Space Shooter Tutorial in Unity - Enemies

by CGAfrica in Tutorials,

on December 27th, 2017

This post is part of series called Boundaries, Hazards and Enemies

Creating a bounding box to destroy any object that leaves the game area. Create an asteroid hazard to challenge the player. Write a controller to spawn our hazards, keep track of and display our score, end the game and allow the game to be restarted. Spawn infinite waves of hazards to challenge our player.

Topics in this series


Boundary



DestroyByBoundary















C# JS BOO

using UnityEngine;
using System.Collections;

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


#pragma strict

function OnTriggerExit(other : Collider)
{
Destroy(other.gameObject);
}










import UnityEngine
import System.Collections


public class DestroyByBoundary(MonoBehaviour):

private def OnTriggerExit(other as Collider):
Destroy(other.gameObject)






 


Creating hazards



RandomRotator















C# JS BOO

using UnityEngine;
using System.Collections;

public class RandomRotator : MonoBehaviour
{
public float tumble;

void Start ()
{
rigidbody.angularVelocity = Random.insideUnitSphere * tumble;
}
}


var tumble : float;

function Start () : void {
rigidbody.angularVelocity = Random.insideUnitSphere * tumble;
}
















import UnityEngine
import System.Collections

public class RandomRotator(MonoBehaviour):
public tumble as single

private def Start():
rigidbody.angularVelocity = (Random.insideUnitSphere * tumble)






 



DestroyByContact


 


 















C# JS BOO

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


function OnTriggerEnter(other : Collider) 
{
if (other.tag == "Boundary")
{
return;
}
Destroy(other.gameObject);
Destroy(gameObject);
}













import UnityEngine
import System.Collections

public class DestroyByContact(MonoBehaviour):
private def OnTriggerEnter(other as Collider):
if other.tag == 'Boundary':
return
Destroy(other.gameObject)
Destroy(gameObject)













 


Explosions



DestroyByContact


 















C# JS BOO

import UnityEngine
import System.Collections
public class DestroyByContact(MonoBehaviour):
public explosion as GameObject
public playerExplosion as GameObject
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()
Destroy(other.gameObject)
Destroy(gameObject)








var explosion : GameObject;
var playerExplosion : GameObject;
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 ();
}
Destroy(other.gameObject);
Destroy(gameObject);
}


import UnityEngine
import System.Collections

public class DestroyByContact(MonoBehaviour):
public explosion as GameObject
public playerExplosion as GameObject

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




 


Game Controller



GameController















C# JS BOO

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);
}


var hazard : GameObject;
var spawnValues : Vector3;

function Start () {
SpawnWaves ();
}

function SpawnWaves () {
var spawnPosition : Vector3= new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
var spawnRotation : Quaternion= Quaternion.identity;
Instantiate (hazard, spawnPosition, spawnRotation);
}


















import UnityEngine
import System.Collections

public class GameController(MonoBehaviour):

public hazard as GameObject
public spawnValues as Vector3

private def Start():
SpawnWaves()

private def Update():
if restart:
if Input.GetKeyDown(KeyCode.R):
Application.LoadLevel(Application.loadedLevel)

private def SpawnWaves() as IEnumerator:
spawnPosition = Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z)
spawnRotation as Quaternion = Quaternion.identity
Instantiate(hazard, spawnPosition, spawnRotation)


 


Spawning waves



GameController 















C# JS 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

private def Start():
StartCoroutine(SpawnWaves())

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)


 

var hazard : GameObject;
var spawnValues : Vector3;
var hazardCount : int;
var spawnWait : float;
var startWait : float;
var waveWait : float;

function Start () {
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);
}
}



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

private def Start():
StartCoroutine(SpawnWaves())

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)


DestroyByTime















C# JS BOO

import UnityEngine
import System.Collections

public class DestroyByTime(MonoBehaviour):
public lifetime as single

private def Start():
Destroy(gameObject, lifetime)


var lifetime : float;

function Start ()
{
Destroy (gameObject, lifetime);
}







import UnityEngine
import System.Collections

public class DestroyByTime(MonoBehaviour):
public lifetime as single

private def Start():
Destroy(gameObject, lifetime)

Previous tutorial in series

cgafrica cgafrica cgafrica cgafrica

Please login to submit your comment