Unity Gaming: Getting Infinite Working

Infinite Runner

Okay so the game I’m making is an infinite runner. What’s an infinite runner. Well it’s pretty self-explanatory but just to make sure everyone is on the same page; an infinite runner game is a game where the world is procedurally generated as the player experiences it, and will exist for as long as the player is alive. Most infinite runners take the title seriously and have their players running along a path that is never ending while collecting prizes and/or dodging enemies.

This unique-ish twist that this example has, is that it’s not going to be 3rd person or 2D like most infinite runner.

Gravity Gunner is a First Person Shooter Infinite Runner.

Platforms

So for our infinite runner we need a character to run constantly. Now you can do this of two ways.

  1. Have the user control your charters speed and movement. While the environment passes by. Think Super Mario Bros.
  2. The player’s speed/position is locked and the user can only control limited direction. Think Temple Run.

In this example the player’s forward direction is locked and the user can only manipulate left/right direction. 

Spawners

Understanding Spawners

Okay so when I say Spawner, what do I mean. Well the Spawner is an object that generates objects, either from scratch or clones of specified objects. In an infinite runner the Spawner refers to the script that will generate the next platform in your series. Now we will use Spawners for our platform generation but it can also be used for generating enemies.

So before we can create our Spawner we should create the objects that we want to Spawn and convert to Prefabs. This way the prebas can be used multiple times in our game.

For an infinite runner I want to create flat platforms for my character to run on. And since the character is going to manipulate “gravity” we will want to spawn four platforms to form a hallway with top, bottom, left and right sides.

So for this piece let’s start simple and build from there.

With any infinite runner its all about the prefabs. The Platform prefabs usually contain.

  • The model for the platform
  • The behavior of that platform, a.k.a whether or not it’s moving
  • Enemy characters
  • Coins or items for the characters
  • Random other triggers for end of level, or game actions

Although some of the items will be generated randomly by their own spawners the location of them in the world usually is dependent on the world/pieces/platforms generated.

Creating Span Object/Platform

So I’m creating my own platform, but you can easily import one from the assets store or from a 3D modeling program. I’m just doing it this way cause it’s faster.

  1.  Create a Cube
    • Make sure it’s at 0, 0, 0
    • I like the dimensions to be 6, 1, 14
  2. Create Prefab
    • It’s really important to maintain organization when building a game. Your Assets folder can become unmanageable really quickly if you don’t have any organization. I’ll talk about it in a later post, but for now trust me. MAKE FOLDERS! I create folders based on the items in my game. For example usually I’ll have a folder for MyPrefabs, MyAudio, MyMaterial, MyScenes, and MyScripts. So in Assets you Should Create a folder called “MyPrefabs”  and to that folder Create a prefab called “SimplePlatform”.
    • Drag Cube Object from the Hierarchy to the Prefab.
  3. Create the Spawner Object
    • Create an Empty Object, Place it at 0, 0, 0.
    • Rename it Spawner.

Okay now let’s make the Spawner do something.

Spawner Script

    1.  Create List
      • This is for keeping track of your objects in case you need to do manipulation to your objects. You know like destroying them, or rotating them…
public List platformList;
public List platformTypes;
    1. Instantiate Platform
      • Now in your Start() method add the following
for(int i = 0; i < 6; i++){
    GameObject x = (GameObject)GameObject.Instantiate (this.PathPlatforms [0], new Vector3 (0, -3f, 0), Quaternion.identity);
    x.tag = bottomPlatformTag;
    x.gameObject.tag = "Platform";
    this.activeBottomPlatforms.Add (x);
}

Destoryers

Understanding Destoryers

So if the spawner and world are the heart of an infinite runner. The destroyer is the ventricles or something, trying to keep this analogy but I might end up switching, we’ll see.
Memory management, especially in games, is the main contributor to glitches and lag. Destoryers help with this by eliminating unnecessary objects from the scene that the engine no longer has to keep track of.

Destoryers and Spawners

Okay so there are many ways to make an infinite runner. I’ll explain one, and then tell you another way.
In ours, the Destoryer works with the Spawner and destroys platforms that are no longer necessary. Instead of having the Spawner spawn platform after platform after platform, we only spawn a set number in order to reduce the number of objects in memory. The destruction of a platform by the Destoryer triggers the Spawner to create another platform. And that’s how we get the “infinite” part of our infinite runner. =D
Although there are other ways to implement the infinite runner, they all consist of some sort of spawner and destroyer. Some Spawners are timed based and will generate platforms based on how much time has gone by, but the destroyer will still exist and destroy objects as they leave the screen.

Creating Destoryers

Create the Destroyer Object by:

  1. Making a cube
  2. Move the cube back to the -20 in the z axis
  3. Scale it to 10x10x1 in the x, y , and z plane
  4. Then take away it’s mesh render

Okay, so now that’s it’s created let’s make it destory the platforms

Destoryer Script

Create another script in the MyScripts folder called “DestoryerBehavior”.
Then add a OnTriggerEnter() method so the Destoryer can be triggered by the objects it intersects. In the Ontrigger add an if statement to check and see if the object setting off the trigger is a platform. If it is, destory it.

    void OnTriggerEnter(Collider other) {

        string collideTag = other.tag;

        if (collideTag == "Platform")
        {
            Destory(other.gameObject);
        }

    }

See super simple. However, we still need to get the Spawner and the Destoryer to work together

Putting it Together

There are two tasks left to complete, 1) Getting the platforms to run into the Destoryer 2) Getting the Destoryer to signal the Spawner to create a new platform.

Moving Platforms

To get the Platforms to collide with the Destoryer we need to add script to the platform prefab that will move the platforms backwards towards the Destroyer.
So on the SimplePlatform Prefab add a new component in the inspector. Create a C# Script called “MoveBackwards”. In it add this script to the update function as well a float for the speed.

    public float backspeed = 3.0f;

    
    // Update is called once per frame
    void Update () {
        transform.Translate(Vector3.back * Time.deltaTime * backspeed);
        }

Script Interction

So to interact with the Spawner the DestoryerBehavior needs to reference the Spawner’s Script. We can do this by Finding the appropriate object and then getting the component we want from the object. Add these fields to the DestroyerBehavior:

    public float backspeed = 3.0f;

    
    // Update is called once per frame
    void Update () {
        transform.Translate(Vector3.back * Time.deltaTime * backspeed);
        }

Now we want to remove the platform the Destoryer collided with from the Spawner’s list, as well as Destory the object.
To do that we need to edit the Destoryer’s OnTriggerEnter method.

        if (collideTag == "Platform")
        {
            
        platformControllerScript.activeRightPlatforms.Remove(other.gameObject);
        Destroy(other.gameObject);

Great so now the Spawner will be automatically triggered to spawn a new Platform.
Run your scene and you should see the platforms being destroyed and spawned so that there are always six. =D

And there you have it. Infinite is now working in your game!
Happy Coding!

-TheNappingKat


2 thoughts on “Unity Gaming: Getting Infinite Working”

  1. for(int i = 0; i < 6; i++){
    GameObject x = (GameObject)GameObject.Instantiate (this.PathPlatforms [0], new Vector3 (0, -3f, 0), Quaternion.identity);
    x.tag = bottomPlatformTag;
    x.gameObject.tag = "Platform";
    this.activeBottomPlatforms.Add (x);
    }

    What is pathplatofrms and active bottom platforms?

    1. PathPlatforms is a List of Platforms that have a path for the player, with no obstacles. The list allows the game to be more dynamic. Active bottom platforms are platforms that are currently generated of type “bottom”. Because the game is a hall way, I tried to make it so that the bottom platforms would always look like a floor, walls would look like walls, ceiling would look like the ceiling (ie. there are no lights attached to the bottomtype platform since lights are not found on the floor).

Leave a Reply

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