- Home /
Question by
Drshk7 · Jan 06, 2014 at 03:10 AM ·
instantiateplayernetworkspawnspawnpoints
Network.Instantiate players at each spawnpoint? Don't use the same spawnpoint?
How would I get it so that I have 5 players spawn in DIFFERENT spawnpoints? I have empty gameobjects as points. I can't use random because players might collide with each other. Help?
Comment
Best Answer
Answer by Bunny83 · Jan 06, 2014 at 03:23 AM
For a prototype we did some time ago (with Unity 2.6) i simply checked the spawnpoints with Physics.OverlapSphere. Just make sure your spawnpoints are not too close. Usually your players are on a seperate layer, so you can pass a layermask to OverlapSphere to only search for players. Just iterate over all spawnpoints, check which ones are "free", put them in a list and randomly pick one from the list.
Example:
// C#
Transform FindSpawnPoint()
{
Transform[] spawnpoints = GameObject.FindGameObjectsWithTag("spawnpoint");
List<Transform> freeSpawnPoints = new List<Transform>();
foreach(var T in spawnpoints)
{
var players = Physics.OverlapSphere(T.position, 2, 1 << PLAYER_LAYER);
if (players == null || players.Length == 0)
freeSpawnPoints.Add(T);
}
if (freeSpawnPoints.Count == 0 )
return null; // no free spawnpoints!!!
else
{
return freeSpawnPoints[Random.Range(0, freeSpawnPoints.Count)];
}
}