How to Create a Simple Unity Game with C#: Jumping Ball, Score and Sound (Beginner Tutorial)

How to Create a Simple Unity Game with C#: Jumping Ball, Score and Sound (Beginner Tutorial)

In this tutorial, you will build a simple endless runner-style Unity game using C#. The player controls a ball that jumps over obstacles, gains score over time, and plays sound effects.

This project is designed for beginners and introduces essential Unity concepts step by step.

Game Features

  • Automatic forward movement
  • Jump with the Space key
  • Obstacles moving toward the player
  • Score system
  • Sound effects

What You Need

  • Unity Hub
  • Unity LTS version
  • Basic C# knowledge

Step 1: Create the Unity Project

  1. Open Unity Hub
  2. Create a new 3D Core project
  3. Name it JumpingBallGame

Step 2: Create the Player

Create a sphere:

  1. Hierarchy → Right-click → 3D Object → Sphere
  2. Rename it to Player
  3. Add a Rigidbody component

Step 3: Create the Ground

  1. Hierarchy → 3D Object → Cube
  2. Rename to Ground
  3. Scale it (X: 20, Y: 1, Z: 5)
  4. Set its Tag to Ground

Step 4: Player Movement and Jump Script

Create a C# script called PlayerController and attach it to the Player.

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float forwardSpeed = 5f;
    public float jumpForce = 7f;
    public AudioSource jumpSound;

    private Rigidbody rb;
    private bool isGrounded = true;

    void Start()
    {
        rb = GetComponent();
    }

    void Update()
    {
        transform.Translate(Vector3.forward * forwardSpeed * Time.deltaTime);

        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
        {
            rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
            jumpSound.Play();
            isGrounded = false;
        }
    }

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = true;
        }
    }
}

Add an AudioSource to the Player and assign a jump sound.

Step 5: Create an Obstacle

  1. Create a Cube
  2. Rename it to Obstacle
  3. Add a Box Collider
  4. Place it in front of the player

Obstacle Movement Script

Create a script called ObstacleMove and attach it to the obstacle.

using UnityEngine;

public class ObstacleMove : MonoBehaviour
{
    public float speed = 5f;

    void Update()
    {
        transform.Translate(Vector3.back * speed * Time.deltaTime);

        if (transform.position.z < -10)
        {
            Destroy(gameObject);
        }
    }
}

Step 6: Score System

Create a UI Text element:

  1. Hierarchy → UI → Text (TextMeshPro recommended)
  2. Rename it to ScoreText

Create a script called ScoreManager:

using UnityEngine;
using TMPro;

public class ScoreManager : MonoBehaviour
{
    public TextMeshProUGUI scoreText;
    private float score;

    void Update()
    {
        score += Time.deltaTime;
        scoreText.text = "Score: " + Mathf.FloorToInt(score);
    }
}

Attach this script to an empty GameObject called GameManager.

Step 7: Collision Sound and Game Over

Update the obstacle to detect collisions.

using UnityEngine;

public class ObstacleCollision : MonoBehaviour
{
    public AudioSource hitSound;

    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Player"))
        {
            hitSound.Play();
            Time.timeScale = 0;
        }
    }
}

Add an AudioSource to the obstacle and assign a hit sound.

Step 8: Testing the Game

  • Press Play
  • The ball moves forward automatically
  • Press Space to jump
  • Score increases over time
  • Collision stops the game

What You Learned

  • Automatic movement
  • Player input handling
  • Basic physics
  • UI and score system
  • Audio integration

How to Improve This Game

  • Spawn obstacles randomly
  • Add background music
  • Create a restart button
  • Increase difficulty over time

Conclusion

This project shows how easy it is to create a complete mini-game using Unity and C#. With just a few scripts, you now have movement, score, and sound.

Unity and C# remain one of the best combinations for beginner game developers in 2026.

Comments

Popular posts from this blog

How to Compare Strings in C#: Best Practices

Do You Really Need Advanced Algorithms to Be a Great Developer in 2025?

Is Python Becoming Obsolete? A Look at Its Limitations in the Modern Tech Stack