Spawning the player in a procedurally generated map
I'm somewhat new to Unity, and was following the tutorial here to make a procedurally generated map. I'm now trying to make it into a maze-type game, and randomly spawn the player in an open area. I found this code from @Gortagia and changed it up so it would work with the current version of Unity. Now my problem is that the player spawns way too high on the map, and constantly overshoots the map entirely, only sometimes spawning in an open space. This code is called when the map is generated.
void SpawnPlayer()
{
GameObject player = GameObject.FindWithTag ("Player");
for (int x = 0; x < width; x ++)
{
spawnLocationX = x;
for (int y = 0; y < height; y ++)
{
spawnLocationY = y;
if (map[x, y] == 0)
{
if (playerCount == 0)
{
playerClone = Instantiate(player, new Vector3(spawnLocationX, -16, spawnLocationY), Quaternion.identity);
playerCount = 1;
Destroy (player);
Debug.Log("Spawn Player");
Debug.Log(spawnLocationX);
Debug.Log(spawnLocationY);
return;
}
if (playerCount == 1)
{
player.transform.position = new Vector3(spawnLocationX, -16, spawnLocationY);
Debug.Log("Move player");
Debug.Log(spawnLocationX);
Debug.Log(spawnLocationY);
return;
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Problems with respawning using a very simple script 1 Answer
Rotate player automatically in an infinite runner to follow a winding road C# 1 Answer
Spawn enemies so they aren't instantiated on top of each other (C#) 2 Answers
Cube seems to bug a little 1 Answer
procedural dungeon generator broken room placement. 0 Answers