Guided Project: Building a Simple 2D Platformer or Endless Runner Game
Creating a simple game is an excellent way to learn Unity and practice your game development skills. In this chapter, we will walk through the process of building a basic 2D platformer or endless runner game.
Example 1: Setting Up the Project
First, create a new 2D project in Unity. Name it “Simple2DGame.” Import necessary assets, such as sprites for the player, ground, and obstacles. Organize your assets into folders for easy management. Create a new scene named “MainScene” and save it.
Example 2: Creating the Player
In the “MainScene,” create an empty GameObject and name it “Player.” Add a Sprite Renderer component and assign a sprite for the player character. Adjust the position and scale of the player as needed. Add a Rigidbody2D component for physics interactions and a BoxCollider2D for collision detection.
Example 3: Writing Player Movement Script
Create a new C# script named “PlayerMovement” and attach it to the Player GameObject. Write the following code to enable basic movement:
csharpusing UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public float jumpForce = 10f;
private Rigidbody2D rb;
private bool isGrounded;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
float move = Input.GetAxis("Horizontal");
rb.velocity = new Vector2(move * moveSpeed, rb.velocity.y);
if (Input.GetButtonDown("Jump") && isGrounded)
{
rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = false;
}
}
}
This script allows the player to move left and right and jump when on the ground.
Setting Up Player Controls and Movement
Now, let’s fine-tune the player controls and add some movement mechanics.
Example 4: Adjusting Movement Parameters
In the PlayerMovement script, adjust the moveSpeed and jumpForce variables to achieve smoother and more responsive controls. You can tweak these values in the Inspector window to find the perfect balance for your game.
Example 5: Adding Animation
To make the player character more engaging, add animations for running and jumping. Import animation sprites, create an Animator Controller, and set up animation states and transitions. Attach the Animator component to the player and assign the Animator Controller. Modify the PlayerMovement script to trigger animations based on player actions.
Example 6: Creating the Ground
Create a new GameObject named “Ground.” Add a Sprite Renderer and assign a ground sprite. Add a BoxCollider2D and Rigidbody2D (set Rigidbody2D to Static). Duplicate the Ground object to create a platform for the player to walk on.
Adding Obstacles, Collectibles, and Scoring Mechanics
Next, we’ll add some gameplay elements to make the game more interesting.
Example 7: Adding Obstacles
Create a new GameObject named “Obstacle.” Add a Sprite Renderer and assign an obstacle sprite. Add a BoxCollider2D and Rigidbody2D (set Rigidbody2D to Static). Place the obstacle in the scene and duplicate it to create multiple obstacles. Create a new script named “ObstacleMovement” to add movement or behaviors to the obstacles if desired.
Example 8: Implementing Collectibles
Create a new GameObject named “Collectible.” Add a Sprite Renderer and assign a collectible sprite. Add a CircleCollider2D (set it to Trigger). Create a new script named “Collectible” to handle the collection logic:
csharpusing UnityEngine;
public class Collectible : MonoBehaviour
{
public int scoreValue = 10;
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
ScoreManager.instance.AddScore(scoreValue);
Destroy(gameObject);
}
}
}
Attach this script to the Collectible GameObject. Create a UI Text element to display the score and create a ScoreManager script to manage the score.
Example 9: Implementing ScoreManager
Create a new C# script named “ScoreManager” and attach it to an empty GameObject named “GameManager.” Write the following code:
csharpusing UnityEngine;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour
{
public static ScoreManager instance;
public Text scoreText;
private int score;
void Awake()
{
if (instance == null)
{
instance = this;
}
else
{
Destroy(gameObject);
}
}
void Start()
{
score = 0;
UpdateScoreText();
}
public void AddScore(int value)
{
score += value;
UpdateScoreText();
}
void UpdateScoreText()
{
scoreText.text = "Score: " + score;
}
}
This script manages the score and updates the UI text.
Example 10: Implementing Game Over and Restart
Create a new C# script named “GameOverManager” and attach it to the GameManager GameObject. Write the following code:
csharpusing UnityEngine;
using UnityEngine.SceneManagement;
public class GameOverManager : MonoBehaviour
{
public GameObject gameOverUI;
void Start()
{
gameOverUI.SetActive(false);
}
void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
RestartGame();
}
}
public void GameOver()
{
gameOverUI.SetActive(true);
Time.timeScale = 0f;
}
public void RestartGame()
{
Time.timeScale = 1f;
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
Create a UI panel for the game over screen and link it to the GameOverManager script. Call the GameOver method when the player dies or the game ends.
By following these examples, you can create a simple yet functional 2D platformer or endless runner game. This hands-on approach helps you understand the basics of game development in Unity, enabling you to create more complex and engaging games in the future.
Leave a Reply