Creating a Mini-Game with C# in Unity

In this tutorial, we'll walk through the process of creating a mini-game in Unity using C#. By the end, you'll have a basic understanding of game development in Unity and be able to build upon it to create more complex games.

Setup

Before we begin, make sure you have Unity installed and a basic understanding of C# programming. Create a new Unity project or open an existing one where you want to build your mini-game.

Concept of the Mini-Game

Our mini-game will be a simple "Catch the Falling Objects" game. The player controls a paddle at the bottom of the screen and tries to catch falling objects using keyboard input.

Implementation Steps

Step 1: Setting Up the Scene

  1. Create a new 2D Unity project or use an existing one.
  2. Create a 2D sprite for the player's paddle and falling objects.
  3. Create a ground object and set up a boundary for the game area.

Step 2: Player Controller Script

Create a C# script to control the player's paddle. Attach this script to the paddle GameObject.

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 5f; // Adjust speed as needed

    void Update()
    {
        float moveInput = Input.GetAxis("Horizontal");
        float moveAmount = moveInput * speed * Time.deltaTime;
        transform.Translate(Vector3.right * moveAmount);

        // Clamp player within screen boundaries
        float screenEdge = Camera.main.orthographicSize * Camera.main.aspect;
        float clampX = Mathf.Clamp(transform.position.x, -screenEdge, screenEdge);
        transform.position = new Vector3(clampX, transform.position.y, transform.position.z);
    }
}

Step 3: Falling Objects Generator

Create a script to generate falling objects randomly from the top of the screen. Attach this script to an empty GameObject or manage it within the GameManager.

using UnityEngine;

public class ObjectGenerator : MonoBehaviour
{
    public GameObject objectPrefab;
    public float spawnInterval = 1f;
    public float spawnWidth = 4f;

    void Start()
    {
        InvokeRepeating("SpawnObject", 0f, spawnInterval);
    }

    void SpawnObject()
    {
        float randomX = Random.Range(-spawnWidth, spawnWidth);
        Vector3 spawnPosition = new Vector3(randomX, transform.position.y, transform.position.z);
        Instantiate(objectPrefab, spawnPosition, Quaternion.identity);
    }
}

Step 4: Object Collection and Scoring

Create a script to handle object collection by the player's paddle and update the score. Attach this script to the falling objects.

using UnityEngine;

public class ObjectCollector : MonoBehaviour
{
    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.CompareTag("Player"))
        {
            // Handle scoring or game logic here
            Destroy(gameObject); // Destroy the collected object
        }
    }
}

Step 5: Game Manager

Create a GameManager script to manage game state, scoring, and game over conditions.

using UnityEngine;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour
{
    public GameObject gameOverUI;
    bool isGameOver = false;

    void Update()
    {
        if (!isGameOver && GameOverCondition())
        {
            GameOver();
        }
    }

    bool GameOverCondition()
    {
        // Define your game over condition here (e.g., time limit, lives lost)
        return false;
    }

    void GameOver()
    {
        isGameOver = true;
        gameOverUI.SetActive(true); // Display game over UI
        Time.timeScale = 0f; // Freeze game time
    }

    public void RestartGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
}

Step 6: UI Setup

Create UI elements such as score display, game over screen, and restart button. Attach the GameManager methods to the appropriate UI buttons for restarting the game.

Conclusion

You've created a simple mini-game in Unity using C#. This basic setup can be expanded upon with additional features like power-ups, levels, and more complex gameplay mechanics. Experiment with different ideas to make your mini-game more engaging and enjoyable.

Links
Unity 6