Dive into the exciting world of game development with Unity, one of the most powerful and versatile engines on the market. Today, we're going to explore how to create an endless runner game, a popular genre that keeps players engaged with its infinite replayability. By the end of this tutorial, you'll have a solid foundation to build your own endless runner game or modify this template to suit your unique ideas.

Before we begin, ensure you have a basic understanding of Unity and C#. If you're new to Unity, check out their official introduction tutorials to get up to speed. Now, let's delve into creating an endless runner game!

Setting Up the Project
The first step in creating any game is setting up the project. For this tutorial, we'll use Unity's 2D mode, as endless runners are typically 2D side-scrolling games. Start by creating a new 2D project and naming it 'EndlessRunner'.

Next, import any necessary assets, such as character models, environments, and obstacles. You can find countless free assets online or use Unity's built-in assets. Once imported, drag and drop your assets into the scene to start building your game world.
Creating the Player Character

A good place to begin is by creating and configuring the player character. For an endless runner, simplicity is key. Pick a simple character model or create one using Unity's built-in 2D shape tools. Rename the object to 'Player' and attach a 'Rigidbody2D' component for physics-based movement.
To control the player, we'll use the arrow keys or WASD. Create a new C# script named 'PlayerController' and attach it to the player object. In this script, use Unity's Input class to capture player input and move the player accordingly. Remember to limit the player's movement to the screen boundaries.
Generating the Endless Environment

One of the defining features of an endless runner is its endless gameplay. To achieve this, we'll create a conveyor belt-style system that continuously generates the environment. Create a new C# script named 'WorldGenerator' and attach it to an empty GameObject named 'Generator'.
In this script, use Unity's Instantiate function to spawn platforms or environment objects ahead of the player. To ensure endless generation, delete platforms from the screen's left side once they're no longer visible. You can also generate obstacles at random intervals to keep the game challenging.
Implementing the Game Logic
![Unity Endless Tutorial • 0 • Introduction [Tutorial][C#]](https://i.pinimg.com/originals/80/a4/fd/80a4fda2fcc9bca889dc637191f0abf0.jpg)
Now that we have the basic mechanics in place, let's add scoring and game over systems to make our endless runner complete.
To implement scoring, create a 'ScoreManager' script and attach it to a GameObject in the scene. In this script, keep track of the player's score as they progress through the game. You can increase the score at fixed intervals or based on the distance the player has traveled.



![[ Tutorial Unity 5 + Android ] Jogo de Runner | Corrida infinita/Endless Run] #1 Iniciando o projeto](https://i.pinimg.com/originals/b4/01/87/b40187d40c172a4941c4e890da18f9c2.jpg)
















Detecting Collisions
To detect collisions with obstacles or the void, Unity's 'OnTriggerEnter2D' function is perfect. Attach a new script to the 'Generator' object, naming it 'CollisionHandler'. In this script, check for collisions using Unity's 'Physics2D.OverlapPoint' function. If a collision occurs, stop the world generation, display a 'Game Over' message, and stop updating the score.
Don't forget to include a restart option to let players try again after a game over. You can attach this functionality to a 'Retry' button within the UI or use Unity's Input class to detect a specific key press for restarting.
Optimizing Performance
Endless runners can quickly become resource-intensive as they generate more and more objects. To optimize performance, destroy objects that are no longer visible and limit the number of objects in the scene at any given time. You can also reduce the polycount of your assets to improve rendering performance.
Additionally, use Unity's occlusion culling feature to hide objects that the camera cannot see, further reducing the workload on your device.
Congratulations! You now have a basic endless runner game set up in Unity. As you continue developing, consider adding power-ups, different characters, and varying environments to make your game stand out. Don't be afraid to experiment with new ideas and make your endless runner truly unique.