Want to make a 2D endless runner in unity? Have an excellent artistic skills but lack programming skills and are stuck in your project? You don’t have to worry, This blog may prove to be a rescue source for you. Here in this blog i will be including scripts for making a 2D endless runner games in unity such as pixel-perfect camera script, object pooling, recycling game object, game manager, time manager to slow down time when the player dies, player animation controller, setting up collider and maintaining their offset depending upon the size of the objects and many more and also a brief description on how you can arrange these scripts with your artworks in the scene. Lets get started….
You can download the full project from here: This sample project is not much about artwork because i have used the character sprites that you can find all over the internet for free, but its all about scripts as i will be expecting you to have some kind of artistic knowledge basically on how to create game sprites.
Import the unity package file into unity and open the demo scene…..
In the hierarchy you can see few objects such as Main camera, foreground, background, spawner as the child object of the foreground from where you can spawn random obstacles, a couple of empty game objects named Game Manager and Time Manager which have game manager and time manager scripts attached to them respectively…
So, lets have a brief look at how to organize the scene…
1. First drag your background sprites to the scene.different scripts, and how they function in the game:
please set all of your artworks to have a pixel to unit ratio of one, filter to point, backgrounds and foregrounds to repeat or tiled, and finally set the pivot point for your obstacles sprites to bottom.
1. Scripts for parallax scrolling
you can simply create background and foreground sprites at different distance from the camera, and attach the TiledBackground and animated Texture scripts and adjust the movement value to your preference(if your background consists of scenes such as buildings or forest than i recommend you to not attach the tiled background scripts into your background, but if your background is made up of brick walls or something like glass panels, it would be perfect to attach the tiled background script to them.)… The parallax scrolling is up and running now.
2. Pixel-Perfect Camera script
this scripts automatically resizes the resolution of your game to best match the resolution of the screen of the device that on which the game is running. Attach this script to the main camera and you are good to go…
3. scripts for object pooling, Destroying object off-screen, Recycling game objects and instant velocity script:
These scripts are mainly associated with the behaviors of the obstacles, so that they can scroll along with our ground, get destroyed after reaching certain distance off the screen and recycle them again, and adjust their movement speed in the game.
can also be used to player (esp destroy off screen and recycle to make player die and reappear)
3.1. How to use them..
Create a empty game object named spawner and attach that empty game object to the right end of the scene a bit far from the point where your background sprites ends in the scene. Make this spawner a child of background and attach the spawner script to it. Create a single prefab of any artworks that you have made to use as a obstacle in the game and attach obstacles script to it.. and when you attach obstacles scripts into that obstacle prefab, have a look at the inspector panel of that script. You can see a line named as sprites.. Drag all of your remaining obstacles sprites into that line and click apply to set up the final game obstacle. By doing this the obstacles scripts will itself take care of duplicating it to different sprites that you attached to that prefab obstacle game object, automatically adjust the collider to them and also adjust the offset of the collider to exactly match the dimensions of the sprites, thus turning every sprites into prefabs and saving the time to manually convert each and every sprites into a prefab. Now, Add instant velocity script to the obstacle prefab(put negative value of instant velocity x in the inspector panel so that it can move from right to left direction in screen.) so that it can move towards the player and you can tweak that velocity going inside of the inspector, add the script named as destroy off screen and recycle game object, and adjust the offset value inside of destroy off screen script,(this basically determines the distance from the screen that the obstacles need to get to get destroyed.)
and recycle game object simply re-adds that destroyed game object into the object pool system so that they could be used again… Avoiding the cluttering of hierarchy and solving issuses of optimization.
4. Scripts for game manager and time manager:
Create a couple of empty game objects, set their transform to default and name them time manager and game manager respectively and attach the scripts of same name to them respectively.
these scripts control various properties of the game such as displaying UI and text on screen, Manipulating time in events (such as death of player), Recycling player(restarting game when player dies and so on.)
and drag your player prefab to the prefab section of the game manager script.
5. Scripts for player game object:
Scripts named InputState, Jump which deal with the position of the player(state of the player in the game) and make player jump to avoid the obstacles.. or can manipulate this scripts to induce sliding too.
Player animation Manager deals with switching between animations with code.(but you need to set up animation and configure animator for your player sprites)
please have a look at Demo-Scene for how to use this assets in your game, and if its still not clear to you please feel free to contact me personally at firstname.lastname@example.org and i will provide you step by step tutorials on setting up the scene, because trust me, if you have a set of brilliant artworks, you can make a brilliant 2D endless runner game in less than half an hour with these scripts.
Demo video on how the final Demo project looked (You can make much much better than this with better Artworks and UI.)
(Note : by default the player is set to jump when space key is pressed, but you can always change it by change if(input.getkeydown methoad ) and add various other inputs for sliding attacking etc. and again if you have any problem with setting the scripts and scenes please feel free to contact me personally at email@example.com, i will be more than happy to help you guys in any way possible. and i will soon be updating package with better artworks, menu selection screen and many GUI’s and other scripts, so please follow me and stay tuned for more.)