Unity is one of the most popular game development engines, widely used by beginners and professionals alike to create 2D and 3D games. This tutorial is designed for complete beginners with no prior experience, guiding you through building a simple 2D game—a “Runner” game where a character avoids obstacles. By the end, you’ll have a working game and a solid foundation to expand your skills. Let’s get started!
Step 1: Set Up Your Environment
- Download Unity Hub: Visit unity.com and download Unity Hub, the tool to manage Unity versions.
- Install Unity: Open Unity Hub, sign in (or create a free account), and install the latest LTS (Long Term Support) version (e.g., Unity 2022.3 LTS as of April 2025). Choose the “2D” core module during installation.
- Create a Project: Launch Unity Hub, click “New Project,” select the “2D” template, name your project (e.g., “RunnerGame”), and choose a save location. Click “Create.”
- Explore the Interface: Familiarize yourself with the Unity Editor—Scene view (game world), Game view (playable preview), Hierarchy (game objects), and Inspector (object properties).
Step 2: Create the Player
- Add a Player Sprite: In the Project window, right-click > Create > Folder, name it “Sprites.” Import a 2D character image (e.g., a PNG from OpenGameArt.org) by dragging it into the Sprites folder.
- Create the Player Object: Drag the sprite from the Project window to the Scene view. This creates a GameObject. Rename it “Player” in the Hierarchy.
- Add a Rigidbody2D: Select the Player, click “Add Component” in the Inspector, and search for “Rigidbody2D.” This allows physics-based movement.
- Adjust Settings: In the Rigidbody2D component, set “Constraints” > “Freeze Rotation” (Z-axis) to prevent unwanted spinning.
Step 3: Implement Player Movement
- Create a Script: In the Project window, right-click > Create > C# Script, name it “PlayerMovement,” and double-click to open it in your code editor (e.g., Visual Studio).
- Write Basic Movement Code:
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public float jumpForce = 5f;
private Rigidbody2D rb;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
// Horizontal movement
float moveInput = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y);
// Jump
if (Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}
}
- Attach the Script: Drag the “PlayerMovement” script onto the Player object in the Hierarchy. Adjust “moveSpeed” and “jumpForce” in the Inspector to your liking.
Step 4: Design the Ground
- Create Ground Sprite: In the Sprites folder, import a ground image or use a simple square sprite. Drag it into the Scene, name it “Ground.”
- Add a Box Collider 2D: Select the Ground, add a “Box Collider 2D” component to define its collision area. Resize it in the Scene view to match the sprite.
- Make it Static: Ensure the Ground is immovable by unchecking “Is Kinematic” in any Rigidbody2D (if added) or leaving it without one.
Step 5: Add Obstacles
- Create Obstacle Prefab: Import an obstacle sprite (e.g., a rock or enemy), drag it into the Scene, and name it “Obstacle.” Add a “Box Collider 2D.”
- Make it Move: Create a new script called “ObstacleMovement,” attach it to the Obstacle, and add this code:
using UnityEngine;
public class ObstacleMovement : MonoBehaviour
{
public float speed = 3f;
void Update()
{
transform.Translate(Vector2.left * speed * Time.deltaTime);
if (transform.position.x < -10f) // Remove when off-screen
{
Destroy(gameObject);
}
}
}
- Convert to Prefab: Drag the Obstacle from the Hierarchy to the Project window to create a Prefab. Delete the Scene instance.
Step 6: Spawn Obstacles
- Create a Spawner: Add an empty GameObject (right-click Hierarchy > Create Empty), name it “ObstacleSpawner.”
- Write a Spawner Script: Create a “Spawner” script and attach it to the Spawner:
using UnityEngine;
public class Spawner : MonoBehaviour
{
public GameObject obstaclePrefab;
public float spawnInterval = 2f;
void Start()
{
InvokeRepeating("SpawnObstacle", 0f, spawnInterval);
}
void SpawnObstacle()
{
Instantiate(obstaclePrefab, new Vector2(10f, -1f), Quaternion.identity);
}
}
- Configure Spawner: Drag the Obstacle Prefab into the “Obstacle Prefab” field in the Inspector. Adjust “spawnInterval” as needed.
Step 7: Add Collision Detection
- Detect Collisions: Modify the PlayerMovement script to handle game over:
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Obstacle"))
{
Debug.Log("Game Over!");
Time.timeScale = 0; // Pause game
}
}
- Tag the Obstacle: Select the Obstacle Prefab, add a tag (“Obstacle”) via the Inspector, and apply the change.
Step 8: Test and Polish
- Playtest: Click the Play button in Unity to test your game. Move the player with arrow keys or A/D, jump with Space, and avoid obstacles.
- Add a Background: Import a background image, drag it into the Scene, and set its “Order in Layer” to -1 in the Sprite Renderer for proper layering.
- Improve UI: Add a “Game Over” text using the TextMeshPro component (install via Package Manager if needed) to display when colliding with an obstacle.
Step 9: Build and Share
- Build the Game: Go to File > Build Settings, switch to the PC/Mac platform, click “Build,” and choose a folder to save your executable.
- Test the Build: Run the .exe file to ensure it works outside Unity.
- Share: Upload to platforms like itch.io or share with friends to get feedback!
Step 10: Expand Your Skills
- Add Scoring: Use a script to increment a score based on time or obstacles avoided.
- Improve Graphics: Use Unity’s Asset Store for free sprites and sounds.
- Learn More: Explore Unity tutorials on Unity Learn for advanced topics like animations and multiplayer.
Tips for Success
- Save frequently (Ctrl+S) to avoid losing progress.
- Use the Unity Documentation for troubleshooting.
- Join the Unity community on Discord or Reddit for support.
By following these steps, you’ll have created a functional 2D Runner game, gaining hands-on experience with Unity’s core features. This foundation sets you up to explore more complex projects, making 2025 the perfect year to kickstart your game development journey!