The code that brings a video or computer game to life is game programming. Programming has changed in tandem with technology. Early on in the history of video games, programmers were often in charge of all development elements, including design. As hardware and software improved, development teams rose significantly.
This tutorial on "How to create a game in Unity" will make you accustomed to working with Unity 3D Software and Game Programming.
What Is an Endless Runner Game?
Endless Runner is a video game where the player collects points and avoids obstacles while continuously going ahead. The primary aim is to reach the level's conclusion without colliding with or crashing into the obstacles. However, the level often repeats indefinitely, increasing in complexity until the player collides with an obstacle.
With this good brief on the Endless Runner, you must create your own Endless Runner Game in Unity 3D.
3D Runner Game Project Creation
You must create a New Project.
You must then open your Unity Hub and click on New Project.
Then, you will select 3D and name your Project 3DRunner. Choose where you want to create your project, and click on Create project.
Now, you will wait for unity to load up the project files and open the editor.
You must create a few folders like Prefab, Materials, and Scripts.
And this is your 3D Runner Game Project.
Now, go ahead and create a platform for your Player to run.
3D Runner Game Platform Creation
You will start by creating a tiled platform that will be saved later in Prefab.
1. You must create an empty game object and name it PlatformPrefab.
2. Create a new Cube by right-clicking in the PlatformPrefab Object and selecting 3D Object to choose Cube.
3. You will name this cube Platform, set its position to (0, 0, 0), and scale to (8, 0.4, 20).
Now that you have created a platform, you can create some obstacles for player objects to crash.
3D Runner Obstacles Creation
You will have six obstacle modifications, but you can make as many as you need:
- You will make Six GameObjects inside the "PlatformPrefab" object and label them "Obstacle1", "Obstacle2", "Obstacle3", and so on.
- Create a new Cube and place it inside the "Obstacle1" object for the first Obstacle.
- You must name this new cube Obst1-Cube1 and scale the Cube to (8, 0.5, 3). And place it on the platform.
- You will make a new material, name it "BlackMaterial," and modify its color to Black.
- Then, you must assign it to the Obst1-Cube1 (this is so that the Obstacle is distinguished from the main platform).
- You must make three cubes for the "Obstacle2" leaving one free spot at the bottom (the player is required to crouch to avoid this Obstacle).
- You will name two cubes Obst2-Cube1 and Obst2-Cube2. And scale them as (1, 8, 1.5).
- We must go on to name third cubes Obst2-Cube3 and scale them as (6, 7, 1.5).
- You must make a new material, name it "YellowMaterial," and modify its color to yellow. Then, you must assign it to Obstacle2 (this is so that the Obstacle is distinguished from the main platform).
- And here is the complete obstacle 2.
- Next, build four cubes for Obstacle3, with one open area at the bottom and one in the middle so that the player can either jump or crouch.
- You must name two cubes Obst3-Cube1 and Obst3-Cube2 and scale them to (1, 8, 1.5).
- Then, you must name one Cube as Obst3-Cube3 and scale it to (6, 2, 1.5). It will be at the top.
- Then you must name one Cube as Obst3-Cube4 and scale it to (6, 1, 1.5). It will be at y=1.25 in height.
- You must make a new material, name it "RedMaterial," and modify its color to Red. Then, you will assign it to the Obstacle3 (this is so that the Obstacle is distinguished from the main platform).
- Here is the Obstacle 3
Next, you have Combinations of the above as obstacles 4, 5, and 6.
So, you have obstacle 4 as a combination of obstacles 1 and 2.
So, you have obstacle 5 as a combination of obstacles 1 and 3.
So, you have obstacle 6 as a combination of obstacles 2 and 3.
Now, choose all the Objects inside the Obstacles and change their tag to "Finish." This process is required to detect when the Player and the Obstacle hit each other.
You now have a platform and obstacles. Now let's write a script to activate obstacles randomly.
3D Runner Game Obstacle Activation Scripts
Now, go to the Script folder and create a script "Simp_Platform.cs".
This script will be responsible for activating obstacles randomly.
public class Simp_Platform: MonoBehaviour
public Transform start;
public Transform end;
public GameObject obst; //Objects that contain various forms of obstacles that will be activated at random.
public void ActRandomObst()
System.Random rand = new System.Random();
int randomNum = rand.Next(0, obst.Length);
public void DeactAllObst()
for (int i = 0; i < obst.Length; i++)
- You will attach this script to the PlatformPrefab Object by dragging it.
- Now you must look at platformPrefab's inspector window. It has an obstacle component. So you must drag all obstacles to this component.
Next, write a script to generate the ground infinitely.
3D Runner Ground Generation Scripts
Now, create a script "Simp_GroundGen.cs".
This script will be responsible for infinitely generating the ground and keeping the score.
public class Simp_GroundGen: MonoBehaviour
public Camera mainCamera;
public Transform start; //this will Mark the location where the ground tiles will begin.
public Simp_Platform PlatformPrefab;
public float movingSpeed = 12;
public int tilesToPreSpawn = 15; //no. of tiles should be pre-spawned
public int tilesWithoutObst = 5; //How many tiles at the beginning should not have obstacles, good for warm-up
List<Simp_Platform> spawnedTiles = new List<Simp_Platform>();
int nextTileToAct = -1;
public bool gameOver = false;
static bool gameStarted = false;
float score = 0;
public static Simp_GroundGen instance;
//start is always called before the first frame update
instance = this;
Vector3 spawnPosition = start.position;
int tilesWithNoObstTmp = tilesWithoutObst;
for (int i = 0; i < tilesToPreSpawn; i++)
spawnPosition -= PlatformPrefab.start.localPosition;
Simp_Platform spawnedTile = Instantiate(PlatformPrefab, spawnPosition, Quaternion.identity) as Simp_Platform;
if(tilesWithNoObstTmp > 0)
spawnPosition = spawnedTile.end.position;
// The Update is always called once per frame
// We will Move the object upward in world space x unit/second.
//Increase speed the higher score we get
if (!gameOver && gameStarted)
//This will increase the speed as time passes
transform.Translate(-spawnedTiles.transform.forward * Time.deltaTime * (movingSpeed + (score/500)), Space.World);
// this will keep track of score
score += Time.deltaTime * movingSpeed;
if (mainCamera.WorldToViewportPoint(spawnedTiles.end.position).z < 0)
//We will Move the tile to the front if it's behind the Camera
Simp_Platform tileTmp = spawnedTiles;
tileTmp.transform.position = spawnedTiles[spawnedTiles.Count - 1].end.position - tileTmp.start.localPosition;
// if the game is over or did not start, then this will prompt us with instructions to press space to play again
if (gameOver || !gameStarted)
// if the game is over, then it will show the current score and give instructions to restart
//this will Restart the scene displayed currently
Scene scene = SceneManager.GetActiveScene();
//This will Start the game file again
gameStarted = true;
// A function responsible for displaying instructions and score
// if game over
// to set the text color red
GUI.color = Color.red;
// dimension for the text box
GUI.Label(new Rect(Screen.width / 2 - 99, Screen.height / 2 - 99, 198, 198), "Your Game is Over\nYou scored: " + ((int)score) + "\nYou can Press 'Space' to restart the game");
GUI.color = Color.red;
GUI.Label(new Rect(Screen.width / 2 - 99, Screen.height / 2 - 99, 198, 198), "You can Press 'Space' to start");
// this will display real time score in green color
GUI.color = Color.green;
// this will display the score
GUI.Label(new Rect(5, 5, 200, 25), "Your Score: " + ((int)score));
- You need to construct two GameObjects for the Start Point and End Point, which are placed at the start and end of the platform.
- Now, you will add these objects to PlatformPrefab's Simp_Platform component.
- Now you must create a game object and name it SimpGroundGen. And you will attach Simp_GroundGen.cs to this game object.
- You will Change the Main Camera position to (10, 1, -9) and change camera rotation to value (0, -55, 0)
- You must create a new GameObject, call it "StartingPoint," and change its position to (0, -2, -15).
- Now you must go to the prefab Folder, and you will drag the PlatformPrefab game object to this to create a prefab.
- Now, you must delete the PlatformPrefab from the hierarchy tab. And your "PlatformPrefab" Prefab is ready.
- Now, you will select the "SimpGroundGen" Object, and in Simp_GroundGen, we will assign the Main Camera to the camera, the StartingPoint Object to the Start Point, and the PlatformPrefab to the Prefab.
Now that you have created the game's complete environment, create a player object.
3D Runner Player Object Creation
Now, you will create a “PlayerObject” Game Object.
You will then create a new sphere by right-clicking on the PlayerObject Object and selecting 3D Object to choose Sphere.
You must make a new material, name it "BlueMaterial," and modify its color to Blue.
Then, you will assign it to the Player.
Now you will remove Sphere's Sphere Collider component.
Now, you have created the player object. Next, create a script to control the Player.
3D Runner Player Control Scripts
Now you will create a new script called "Simp_Player.cs". This script will let the Player jump when you press the W key and crouch when you press the S key.
public class Simp_Player: MonoBehaviour
public float gravity = 20.0f;
public float jump_Height = 2f;
bool grounded = false;
bool crouch = false;
//method start is always called before the first frame update
r = GetComponent<Rigidbody>();
r.constraints = RigidbodyConstraints.FreezePositionX | RigidbodyConstraints.FreezePositionZ;
r.freezeRotation = true;
r.useGravity = false;
defaultScale = transform.localScale;
if (Input.GetKeyDown(KeyCode.W) && grounded)
r.velocity = new Vector3(r.velocity.x, CalculateJumpVerticalSpeed(), r.velocity.z);
crouch = Input.GetKey(KeyCode.S);
transform.localScale = Vector3.Lerp(transform.localScale, new Vector3(defaultScale.x, defaultScale.y * 0.4f, defaultScale.z), Time.deltaTime * 7);
transform.localScale = Vector3.Lerp(transform.localScale, defaultScale, Time.deltaTime * 7);
// Update is called once per frame
// We apply gravity manually for more tuning control
r.AddForce(new Vector3(0, -gravity * r.mass, 0));
grounded = false;
grounded = true;
// From the jump height and gravity, we deduce the upwards speed
// for the character to reach the apex.
return Mathf.Sqrt(2 * jump_Height * gravity);
void OnCollisionEnter(Collision collision)
if(collision.gameObject.tag == "Finish")
Simp_GroundGen.instance.gameOver = true;
- You must attach the “Simp_Player” script to the "PlayerObject" object.
- You will click on add component in PlayerObject's inspector tab, select Physics, and choose BoxCollider Component to the "PlayerObject" object.
- Finally, place the "Player" object slightly above the "StartPoint" object, right in front of the camera.
Now, you can Press W to make the player Jump.
Now you can Press S to make the player Crouch.
With this, you have successfully created your first Game.
Master front-end and back-end technologies and advanced aspects in our Post Graduate Program in Full Stack Web Development. Unleash your career as an expert full stack developer. Get in touch with us NOW!
The next lesson in your C# training can be "C# Rest API." REST stands for "Representational State Transfer," meaning REST is a style of building distributed systems based on rules describing and talking about network resources. RESTful services talk to each other through HTTP (HyperText Transfer Protocol). The REST system talks to other systems through web resources identified by URIs.
Simplilearn is the world's most popular online Bootcamp for developing skills in the digital economy, and it's here to assist you in doing just that. Digital marketing and data science are just two topics we cover extensively in our online courses.
Simplilearn teamed with the Caltech CTME and the Indian Institute of Technology, Kanpur, to deliver their Software Development courses. These courses cover the fundamentals of data structures and algorithms and more advanced subjects such as Competitive Programming. As a software engineer, you will learn about data structures such as trees, graphs, and queues.
The comments section below is open for questions and comments about this 'How to create a game in Unity' tutorial. Happy learning!