I have a 2D infinite runner platformer game built in Unity 5, where the level procedurally generates itself. I need to check if it is possible to pass the holes on the level.
How could I do that?
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It only takes a minute to sign up.Sign up to join this community
I understand you are dealing with both box obstacles and gaps. The way it was described, I can see three possible situations. Correct me if the game is designed differently.
With the first, there are no obstacles before the gap, at least no obstacles that the player can jump atop. In this case, you would need only a fix max width for the gap.
In the second, the player can jump atop an obstacle before the gap, increasing his jump distance. This means we can increase the gap width.
The final sketch shows what happens when two obstacles before the gap allow an even higher jumping elevation.
Here is a possible algorithm to use during generation:
When creating a new obstacle: If there is an obstacle before it and it is accessible: If newObstacle height - lastObstacle.y < jumpHeight: Mark the new obstacle as accessible. Else if newObstacle height < jumpHeight Mark the new obstacle as accessible When creating a gap: gapWidth = minimum gap distance If there is an obstacle before that is accessible gapWidth += lastObstacle.y * someConstant
This way, no matter how high the random staircase is, you will know how wide you can make your gap.
Do it at the generator level: Always generate your level using a selection of passable gaps / obstacles section prefabs.
If two successive sections are not passable mark them as such so the generator does not put both in succession and/or adds some space between them.
These sections will also instruct the AIs on how to clear the obstacles.