- Home /
random spawning
I've finished my online multiplayer game just now, but I found this spawning error. My main idea is that when a counter reaches zero, the server takes half the players and make them to props and the other half to hunters. After that the server sends everyone RPC to spawn. This works just fine, but the random selection doesn't working. I debugged it (SetProps method 'r' variable) and within the for loop it gives almost always the same number so there will be just one prop and the rest are hunters. Also the random spawn points do the same. I think it's because the for loop is too fast for generating new random nubmer, but i tried this: put the selection into an enumerator and always wait some times befor a new selection, but it didn't work. And i don't want to put a do-while in it. Here is my part of code:
void StartRound()
{
SpawnTimer.StopTimer();
SpawnTimer.ResetTimer();
isPropWin = true;
if (PhotonNetwork.isMasterClient)
{
SetProps();
SetHunters();
for (int i = 0; i < PhotonNetwork.playerList.Length; i++)
{
photonView.RPC("Spawn", PhotonNetwork.playerList[i], PhotonNetwork.playerList[i]);
}
}
}
void SetProps()
{
// a lista fele veletlenszeruen prop, a tobbi vadasz
// lista alapjan spawnolni mindenkit
for (int i = 0; i < (PhotonNetwork.playerList.Length+1) / 2; i++)
{
Random.seed = System.Environment.TickCount;
int r = Random.Range(0, PhotonNetwork.playerList.Length); // az utolso elemet sosem adja, de ez jo, mert 0-tol indexel a players(ArrayList)
PhotonNetwork.playerList[r].SetCustomProperties(myHashTables.propPlayerType);
}
}
void SetHunters()
{
for (int i = 0; i < PhotonNetwork.playerList.Length; i++)
{
if (PhotonNetwork.playerList[i].customProperties["PlayerType"] == null || (Team)PhotonNetwork.playerList[i].customProperties["PlayerType"] != Team.Prop)
{
PhotonNetwork.playerList[i].SetCustomProperties(myHashTables.hunterPlayerType);
}
}
}
Answer by Mapleman · Apr 26, 2015 at 07:44 PM
Do
Random.seed = System.Environment.TickCount;
before the for-loop, not inside it.
Your answer
Follow this Question
Related Questions
move gameobject into a random position and the spawn an enemy 1 Answer
Spawn enemies so they aren't spawned on top of each other (C#) 1 Answer
Enemies spawn on top of each other(C#)(Unity) 1 Answer
How to make enemy prefab spawn at random times between 1.0f to 10.0f. 1 Answer
Problem with random generated objects(intersects objects) 1 Answer