- Home /
How can I spawn enemies while leaving a gap for the player to advance?
I'd like to have a grid that is 5 units wide that generates rows as the player moves forward. I want my player to advance one unit at a time, whether it's forward, backwards, or side to side. I also want to have 1-2 enemies spawn in each row of the grid without blocking off the player completely.
For example, if you imagine a 5x5 grid, I don't want the first enemy to spawn on (1,1), the second on (2,2), the third on (3,3), fourth on (4,4) and fifth on (5,5) as that would make a diagonal line that doesn't allow the player to progress. For clarification, my player is unable to attack/destroy enemies, they simply have to avoid them.
I know there has to be a way to do this, I just don't know what it is.
I am using C#
Answer by BlakeSchreurs · Oct 05, 2017 at 12:59 PM
Ultimately, you're looking at a maze generation algorithm (of which there are many): https://en.wikipedia.org/wiki/Maze_generation_algorithm
That said, with a grid this size, you don't need to go crazy., since with a 5x5 grid there aren't too many options for backtracking (I can only think of one). IF you're not allowing (or don't care about) back-tracking, here's a simple procedure:
Randomly select if your solution is going to be row-wise or column-wise. Say it chose column-wise. you're going left-->right through the maze, and you're in column 1. You want to go to column 2.
Step 1: Create a mask / list of "No spawn" squares. Populate with player start and goal positions
Step 2: Pick a point in column 2 that the player can get to safely (Perhaps, say +/- 3 rows from current position). "No spawn" the target point.
Step 3: "No spawn" all the cells between player start and the row of that safe place in column 2.
Step 4: Repeat from column 2->3, 3->4, 4->Goal
Step 5: Spawn monsters anywhere not labeled "No spawn"
Your answer
Follow this Question
Related Questions
Unity how to make different enemy spawn one at a time 1 Answer
enemy AI doesnt work when enemy is spawned but does if it is already in the scene 1 Answer
Edit this script so that the player stops at a distance and attack? 0 Answers
c# Enemy's spawning over time, problem 2 Answers
Look at closer to player 1 Answer