Graphics Reference
In-Depth Information
Spawning works the same here as with the other games, although instead of using an
array full of start positions, it only uses a single transform for the spawn position. Unity's
GameObject.Find() function is used to look for a gameObject named Player_Parent_
Object. Its transform information will be used on all game levels as the starting point for
the players:
// find player parent transform
playerParent= GameObject.Find("Player_Parent_Object").transform;
Vector3[] playerStarts= new Vector3[totalPlayers];
Quaternion[] playerRotations= new Quaternion[totalPlayers];
As with the other example games, the SpawnController SetUpPlayers() function is
used to instantiate players into the game. It requires an array of positions and an array of
rotations, which are made here. This game only has a single start point and not an array
of start points as other games might have, but it uses the same system purely for the sake
of consistency throughout the topic. Of course, if you are the reader who is going to take
this game and add 50 players to it, then this is, no doubt, going to be useful to have:
for(int i=0; i<totalPlayers; i++)
{
tempQuat= Quaternion.identity;
if(i==0)
{
// place player 1 at the default start position of
// -5,0,0
tempVec3= new Vector3( -5, 0, 0 );
} else {
// we'll make player 2 a start position 5 units to
// the right of the start position of player 1
tempVec3= new Vector3( -5 + (i*5), 0, 0 );
}
playerStarts[i]=tempVec3;
playerRotations[i]=tempQuat;
}
The players are only spawned when didInit is still false. In this game, the players per-
sist throughout the whole game, so we need to make sure that players only spawn once to
avoid chaos in the scene:
// if we haven't already got players set up, didInit will
// still be false... otherwise we skip creating the players
if(!didInit)
SpawnController.Instance.SetUpPlayers( playerPrefabList,
playerStarts, playerRotations, playerParent, totalPlayers );
The reference held by the variable playerGO1 will be used later. SpawnController will
return a gameObject from its GetPlayerGO() function based on the index passed in as a
parameter to it:
// grab a ref to the player's gameobject for later
playerGO1= SpawnController.Instance.GetPlayerGO( 0 );
Search WWH ::




Custom Search