Graphics Reference
In-Depth Information
4.3.1 A Simple Spawn Controller
The reason we use a spawn controller is so that we can track what has been added to the
game world, if required, or switch out the spawning system for something more robust
(such as a third-party library from the Unity Asset Store like PoolManager).
The spawn controller we will build in this chapter will have a few extra functions
that may be useful to game development such as SetUpPlayers, which is a function that
takes a list of prefabs to instantiate, a list of start positions, start rotations, and the total
number of players. The function will instantiate all of the players at the correct positions
and rotations:
public void SetUpPlayers (GameObject[] playerPrefabs, Vector3[]
playerStartPositions, Quaternion[] playerStartRotations, Transform
theParentObj, int totalPlayers)
An array of player prefabs can easily be set up and passed into the spawn controller to
instantiate, letting the spawn controller take care of building them rather than having to
write instantiation functions for each game. For example, the racing game example Motor
Vehicle Doom will have an array of vehicles that may be set up in the Inspector window of
the Unity editor. This array gets passed to the spawn controller and instantiated. By deal-
ing with players in this way, we can easily switch out player types between levels or even
build the list dynamically if required.
Below is the SpawnController.cs script:
using UnityEngine;
using System.Collections;
public class SpawnController : ScriptableObject
{
private ArrayList playerTransforms;
private ArrayList playerGameObjects;
private Transform tempTrans;
private GameObject tempGO;
private GameObject[] playerPrefabList;
private Vector3[] startPositions;
private Quaternion[] startRotations;
// singleton structure based on AngryAnt's fantastic wiki entry
// over at http://wiki.unity3d.com/index.php/Singleton
private static SpawnController instance;
public SpawnController ()
{
Search WWH ::




Custom Search