Making an Endless Runner game by Unity3D (Step by Step)

by:

Gaming

Maybe you know that nowadays all the endless runner games like Run 3 are really attractive because of highly addictive gameplay and challenges. In this article, I will guide you how to simply make an Endless Runner 2D by Unity3D software. (There is no requirement of knowledge about programming). For game “Run3”: This is a 3D game that requires understanding about advanced programming, so I will not mention it in guideline. I will perform separated articles talking about 3D game next time.

game zombie tsunami

(Photo: game zombie tsunami)

Part 1: Preparation

Firstly, you dowload and install unity software by the following link: https://store.unity.com/
Just choose a free version, we can use most essential functions to make game

install unity software

After installing and restarting software, window frame and load projects will appear

load projects unity

Next step, click the On Disk button (to be able to work offline and not need internet connection). Then click the New button to create the project.

create project unity

After that, wait a minute, working screen will appear

unity working screen 1

Here you maybe a little bit confused, look at this photo to easily understand

unity working screen 2

Next, learn about some basic tools for customizing an object.

unity basic tools

In left-to-right order: change the place to look – move objects – rotate objects – customize the object’s elasticity, size – customize elasticity of objects.

Several terms will be used in the article and notes

Object (now called Object or obj): Something in the game. For example, characters, sun, cat, wallpaper.

Sprite: just a picture no more no less. When the sprite is attached to the obj, the obj will take the shape of the sprite. It can be regarded as the appearance of obj. Just like a person’s “skin”.

Script: file code. If the sprite is considered as skin, the script is the brain. All things we programming for objects like hitting enemy, touching the screen, jumping, etc are in here.

Component: Things making obj as sprite, script are all components. It is possible to temporarily call a component as “part”.

*Note:
– save when leaving unity
– during making games, when getting problems, you can refer to my project:

https://www.dropbox.com/s/1po4p79u00ribs8/Endless%20Run.rar?dl=0

You should dowload these pictures, because the article does not include how to draw characters or landscape, so I already prepared essential pictures (sprite)

http://www.gameart2d.com/uploads/3/0/9/1/30917885/jackfree.zip
and
http://www.gameart2d.com/uploads/3/0/9/1/30917885/deserttileset.zip

After downloading, extract files and the only one we need is png file, the rest you can delete.

Next, go to Unity3d and create the following folders by right-clicking in the Assets pane (if you are wondering the place of assets pane, please look at the upper left corner of each frame to find the name) and choose create -> folder:

Sprite

Script

Scene

Animation

making an endless runner game 1

Next, enter the png file you just downloaded into unity by: Opening the file browser in thumbnail mode and dragging the png file into the sprite folder in unity

making an endless runner game 2

Next, in unity, choose File->build settings-> android mode -> press switch flatform

making an endless runner game 3

At game window, replace free aspect with 800×480

making an endless runner game 4

Congratulation, now you are ready to make game

Part 2: Making game frame

Open the Sprite folder-> png-> Tile. Here you will see the pictures of the ground. Drag and drop the images into the left Scene pane and use custom tools to create your desired terrain.

making an endless runner game 5

Now, press any block at Hierarchy frame and then look at Inspector frame. If you see 2 components like me, you are doing correctly.

making an endless runner game 6

Let’s talk a bit about component trasnform.

Position: position of the obj in the scene

Rotation: Rotation of obj.

Scale: Elasticity of obj.

In addition to using tools that I have mentioned above, you can completely customize the above parameters to create the desired terrain.

 

Next, go to the png folder, drag and drop BG (background) to the scene frame. In the component sprite renderer of BG, find the order in layer and set it to -10. You can imagine that this is the display priority of the game, when an obj has a large number of order in the layer will be displayed first and over the obj having a small one. Because our BG needs to be background, so it must be smaller number of order in layer than the other objects.

making an endless runner game 7

Then, select create empty in the scene frame and named matdat, then drag all the ground obj to matdat. Now they already become sub object of matdat. This means, when you move matdat, all obj will move as well.

making an endless runner game 8

Now you select all ground objects. Press component->physic2D->Polygon Collider 2D. This is to create frame in order that objects can stand on the ground without being went through.

making an endless runner game 9

Part 3: Making characters

Do you see images from run (1) to run (8)? Select all of them by pressing Shift and clicking run (1) and run (8). Drag and drop and scene. A frame of animation will appear and you save them in the animation folder.
The character maybe quite large, please adjust accordingly. Here I choose the scale as (0.3,0.3,1)

making an endless runner game 10

Similar to the above ground, add the component box collider2D to the character.

Next, add the Rigidbody2d component to the character. This component causes the character to suffer the effect of gravity.

Remember to rename the character to easily manage, preferably run (1) should be changed into character.

making an endless runner game 11

Now, press play and enjoy your achievement. The next part will be more difficult

making an endless runner game 12

Part 4: Writing Script

In this part, I will guide you how to write scripts. If you have a little knowledge of programming, it will be easy to understand, can even change the source code, and otherwise follow instruction.
In the Script folder, right-click and select create-> C # Script and name that file as character.

making an endless runner game 13

Then double-click on the file and wait for a while, the script editor will pop up. (For some devices, the script editor has a different interface, but it’s fine.)

making an endless runner game 14

Now, write like me
// all sentences written in this form are interpreted as explanations, the computer will not read


Now press Ctrl + Shift + S to save the file. You go back to unity, drag the script character file into the obj character (or click add component in the inspector frame and type “character” in the search box).
Next, look at component rigidbody2d, press constraints and tick and z box (so that the characters are not rotated if the force is applied.)Now press Ctrl + Shift + S to save the file. You go back to unity, drag the script character file into the obj character (or click add component in the inspector frame and type “character” in the search box).
Next, look at component rigidbody2d, press constraints and tick and z box (so that the characters are not rotated if the force is applied.)

Press play and enjoy your achievement

Now, back to the script character file, we will write a script that allows jump when the player touches the screen.

Save and then play to test

Finally, you drag 2 main camera and BG obj to character obj to create sub object. This allows the character to move, BG and main camera move as well.

making an endless runner game 16

In the Assets file, create the material folder. Select create-> Physic materia 2D. Adjust both of the material’s parameters to 0.

making an endless runner game 17

Select all the ground objects, drag and drop the new material into material frame of collider2D.

making an endless runner game 18

Part 5: Writing endless runner script

Drag any sprite file into the scene frame (mine is StoneBlock), set a large scale x (about 1000). Drag the obj to the right edge of the camera. Do the same with the lower border. (These 2 obj play the role as a limited line of the terrain.)

making an endless runner game 19

Add polygon collider2D to 2 above objects.

Let’s make the terrain longer by adding ground obj and, add component polygon collider2D, add material, set it as sub obj of ground. In addition, you can also use other obj in the object folder to decorate (this decoration obj does not add the component polygon collider2D in order to the character can go through, but still be sub object of matdat).

making an endless runner game 20

In order in layer of character obj, change to 10.

Please click create empty, named it as checker1, then add component box collider2D, tick into is trigger. Edit it in the right border of the camera or BG like in the above photo. Obj is set as a sub obj of character

making an endless runner game 21

Look at the inspector, click on the tag, select add tag, click +, name the tag as the checker. Click on checker1 obj, and then add tag for this obj as checker.

making an endless runner game 22

Create another obj and name it as checker2. Set as a sub object of matdat. Continue to add box collider2D, tick into is trigger. Place this obj at the bottom of terrain.

making an endless runner game 23

The idea is that if two checkers go through each other, they will create a matdat object clone just behind matdat obj. By that way, the character will run endlessly (because terrain is constantly being created.)

Now create a script file named matdatspawn in the script folder. Open it up and coppy this code.

The Instance statement means creating a clone obj with the following parameters: (true obj, position, rotation).

The destroy statement means that after a certain period of time (10s), obj (matdat) is destroyed.

 

Now save it and add script just created to checker2 obj in unity.

Right click on obj ground, select duplicate. Move matdat obj(1) horizontally to the spot that you want the obj clone will be created there. Then do a calculation: get the position x of matdat (1) minus the position x of matdat. Then, remember that result (in my case the result is 83.9).

Now remove matdat obj (1).

Continue to remove the 2 obj we have created above.

Return to obj checker2. Look at the component matdatspawn (script). Enter the result you just calculated in the box distancex.

For Matdat, you drag and drop matdat obj into it.

making an endless runner game 24

Finally, choose play and experience game

Part 6: Fixing some errors.

Error 1

When our character stops due to being trapped by blocked stones, the camera stops, I do not want this situation. So I’ll fix it.

First, you pull the main camera away from sub obj of character.

Next, drag 2 BG obj and checker1 into sub obj of main camera.

making an endless runner game 25

In the camera inspector, add 2 components including character script and Rigidbody2D.

In the jump frame, enter 0 digits. In Rigidbody2D, select kinematic in body type.

making an endless runner game 26

And then you drag BG objects, character, and main camera to middle of the screen (in order left to right).

Error 2

Character can still jump in the air.

First, create a tag named ground

Attach the newly created tag to all ground objects.

Copy this script into the script character file.

Part 7: Completing the game

Press the ctrl + shift + S. Save this scene in the scene folder and name it play.

making an endless runner game 27

Press ctrl+shift+B. In build setting, press add open scene.

making an endless runner game 28

Now, create first enemy

Drag the sprite cactus into the scene. Add polygonm collider2D, add them an enemy tag. Let them be a sub object of matdat

Edit characterscript file.

Now we create pause button

Right click-> ui-> button.

In the text, drag down the text and change it into pause.

making an endless runner game 29

For canvas, change render mode into screen space-camera. Drag and drop main camera into it.

For UI scale mode, change into scale with screen size

making an endless runner game 30

Now drag that pause button to the place as you want

making an endless runner game 31

Create a script file named pause. Drag into the inspector of button pause:

Drag pause button into your button

making an endless runner game 32

Create 1 cactus obj like above, but drag its position to the bottom so that when the character falls down, it will die. Set scale x with large size

making an endless runner game 33

Part 8: Exporting apk file

References: http://hocunity.3dvietpro.vn/default.aspx?g=posts&t=265#post3937

Download 2 following files.

Sdk : https://developer.android.com/studio/index.html

Jdk : http://www.oracle.com/technetwork/java/javase/downloads/index.html

Extract.

Menu->edit->References

Browse the two folders just downloaded above to the Sdk and Jdk.

making an endless runner game 34

Ctrl+shift+B. Select Player settings.

Scroll down to the other settings section and edit the package name (as opposed to default).

making an endless runner game 35

Done, press build

Some references you can learn about unity

https://unity3d.com/learn

https://www.howkteam.com/

Leave a Reply

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