- Home /
I figured it out
Preventing enemies from spawning in walls
Hello, so I was using the 2d roguelike tutorial and then the random dungeon generation tutorial here on the unity website, but the dungeon generation just sort of gave the idea of how to randomly spawn enemies without really trying it. I have the code he used, but the problem is that it spawns enemies inside of walls. I am wanting to know how to prevent that from happening. Here is the method that instantiates the objects (it is used for both the enemies and the food) void InstantiateFromArray (GameObject[] prefabs, float xCoord, float yCoord) { // Create a random index for the array. int randomIndex = Random.Range(0, prefabs.Length); // The position to be instantiated at is based on the coordinates. Vector3 position = new Vector3(xCoord, yCoord, 0f); // Create an instance of the prefab from the random index of the array. GameObject tileInstance = Instantiate(prefabs[randomIndex], position, Quaternion.identity) as GameObject; // Set the tile's parent to the board holder. tileInstance.transform.parent = boardHolder.transform; }