3D Infinite Runner Game Tutorial – Unity3D (C#)


3D Infinite Runner Game – Unity3D (C#)

infiniterunner

Play Demo

We have 2 choice to move character in an infinite runner game: move character or move ground. First one, you can move ground continuously in a negative direction to character and move right or left your character. I will explain the second one in this tutorial that a stable ground and a moving character.

For a brief description, we will create a plane as road, we will create some spheres to create coins and to make character move(it will follow the stable spheres and we will get a moving action) with Lerp method.

At first, create a Directional Light and a A plane with size (0.5, 1, 10). Create a sphere named P1 at the begining of plane( 0, 2, -5). This sphere is our start point. Now duplicate it, rename it as  P2 and replace it to (0, 2, -4.5). Character will move from P1 to P2, from P2 to P3, from P3 to P4 etc. So, create and place 20 spheres with same Z distance (0.5). Create an empty game object named Points and regroup these spheres.

Screen Shot 2015-03-24 at 15.29.29

 

There is no sphere at the end of plane because we will clone this plane and the first sphere of the clone plane will be there. In additional, create a sphere named RoadSphere and place it 0.5Z far from the first sphere as seen on picture. This sphere keeps location of the clone plane. All of these plane and spheres will be its children and and it will be located on the last sphere of current plane. That provides a continuously road for us. Edit hierarchy as RoadSphere>Plane>Points>P1, P2, P3, P4,….. Make it prefab.

For character, create a sphere named CharacterSphere. Create a Capsule for character as child of CharacterSphere. Rotate it 90X for a horizontal capsule. Attach camera to Character sphere to make camera follow character. You can use textures for a better experience. Pick some textures from google and attach it your objects.

Screen Shot 2015-03-24 at 16.02.33

 

The small sphere belongs to our character and the big onScreen Shot 2015-03-24 at 16.06.34e is P2 sphere of road. Now, you can hide these all spheres by clickling checkbox near the MeshScreen Shot 2015-03-24 at 16.08.06 Renderer title. You can select multiple spheres by holding CTRL. Now you get a clear view on game scene. We don’t need view of these spheres and we don’t need  their Mesh Renderers too.

 

 

Let’s start coding. We will 2 scripts for game controller and for collision. Create a C# script and don’t forget to add using System.Collections.Generic
because we will use lists.

This code checks at start if there is a road at scene. If not, checks if there is one road or more. If there are more than one, it destroys the old one. Code keeps all spheres and roads in a list to get information about them later in update method. New road will be added “LevelCount*19+LevelCount-1″th sphere. It is 20th sphere for the first clone after the road which is created at start. Now, call CreateNewRoad method at start.

I said that our character will follow spheres with Lerp method.

We assigned starPoint and endPoint for movement. Also, we use LookAt to check if there is a sphere after a few spheres or not. When our character reaches to 10th sphere, we create a new road. You can change these values but be careful, if you create new road at last sphere, you get an error because code checks 3-4 far spheres and it will be empty. Now, we can add keyboard controllers in Update method.

Create a folder named materialScreen Shot 2015-03-24 at 17.30.07s and create four materials for yellow, red, blue and black material. These materials give coins different colors. Add your materials as public objects to your script. Create a cube as an game object for coins.

 

Screen Shot 2015-03-24 at 17.33.30

 

Now, you may add collider to collect coins or a timer for a better gameplay experience.

You can download script file here.

©Coffee Break Codes – 3D Infinite Runner Game Tutorial – Unity3D (C#)


Leave a comment

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

3 thoughts on “3D Infinite Runner Game Tutorial – Unity3D (C#)