Devblog #2: Infinite Runner Mechanism in AwesomeDillos

isompi

Preface

There are numerous ways to implement an infinite runner style mechanism in Unity. In this Devblog I will show you how we do it in our game, AwesomeDillos and at the same time teach you how to do it – or at least give you ideas on how to do it – in your own game. Let’s begin!

What you will need

To get started, we need 2-5 sprites which will serve as the pieces of the level that we will be generating ad infinitum (of course, the more you have these modules the more diversity you will get in the level). We will need something to represent the player also, so use a sprite or a generic cube. If you don’t have any sprites of your own, you can download and use quality material from www.opengameart.org.

Basic idea

The concept here is that each of the level sprites will be made into a prefab and by constructing different kinds of prefabs from different kinds of sprites, it will create variation to the level. For example, one prefab could be a sprite of a hill, added with power-ups, collectables etc. and another could be a sprite of a trap with its corresponding collectables and such. So it’s sort of a modular approach. You construct different modules and generate them randomly or if you want with some predetermination and that creates the infinite map.

Let’s begin

First of all, create a new folder called “Resources” in your Assets folder. Here we will create another folder called “Prefabs”. Create 5 prefabs into this folder by clicking the right mouse button  Create  Prefab. Then add the sprites to the “Sprites” folder, add them to the scene and drag them to each individual prefab. Now your prefabs folder and your folder hierarchy should look like this:

You can of course name the individual prefabs anyway you want.

The LevelGenerator script

Let’s take a look at the LevelGenerator script that handles the creation of our level.


The script is pretty self-explanatory and we have commented all the important parts. What we basically do is we load all the prefabs into an array (levelPieces) and at appropriate times add parts from the array to the queue (piecesQueue) and also remove the parts that are obsolete (meaning the player has moved past them far enough so they can be deleted).

What keeps track of the parts in the actual scene is the activeParts list which we use delete the correct level pieces in the right order. It’s a pretty simple approach but it’s powerful enough so that we can add the variation to the level that we need. By using prefabs as modules for the map we can add all sorts of things to them and make the level seem different every time you play it.

Remember, you can add child objects to prefabs so the prefab can be more than just a sprite of the level; it can have collectables like power-ups and coins also. You can have predetermination instead of complete randomness in the level by for example constructing arrays with specific prefabs that make up a different sort of section that you want and then queue the whole array at some point of your own choosing.

Player movement

Here’s a small script for the automated player movement so you can see the mechanism in action:

Here’s how it should look when done right (the footage is not from actual gameplay, just the mechanic in action at its most basic state):

FinalGif

So that is basically the mechanism we use in our game, AwesomeDillos. We do use a separate system for the background generation and parallaxing but that’s a topic for another devlog in the near future! Stay tuned for a video version of this devlog where we will explain the mechanism in greater detail, perhaps there is something that you would still like to know more about.

Be sure to follow us on facebook and twitter!

-the Tuiske team

Leave a Reply

Your email address will not be published. Required fields are marked *


9 × = forty five