Spawning waves

Geprüft mit Version: 5.1

-

Schwierigkeitsgrad: Anfänger

Spawn infinite waves of hazards to challenge our player.

Spawning waves

Anfänger Space Shooter tutorial

Please note that this project was developed for Unity version 4. It currently works, and has been checked, with Unity version 5.1. Please turn annotations on while watching the video, as we can add annotations to the videos when there are discrepancies between the recorded content and the current version of Unity.

Get the Upgrade Guide for Unity 5 here.

Please refer to the Official Q&A Page on the Forums for an Upgrade FAQ and to ask any questions.

GameController

Code snippet

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

Code snippet

using UnityEngine;
using System.Collections;

public class DestroyByTime : MonoBehaviour
{
    public float lifetime;

    void 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)

Ähnliche Tutorials

Zugehörige Dokumentation