Skip to content Skip to sidebar Skip to footer

Widget HTML #1

Learn to create a 2D Action Zombie Game in Unity



Learn to create a 2D Action Zombie Game in Unity

In this comprehensive Udemy course, you will delve into the process of creating a Soul Knight-style game using the powerful Unity development platform.

Enroll Now

Creating a 2D action zombie game in Unity can be an exciting and rewarding project. Unity is a powerful game development engine that provides a wide array of tools and features to bring your game ideas to life. In this guide, we'll walk through the process of creating a basic 2D action zombie game. We'll cover the essential steps, including setting up the project, designing the game environment, creating player and zombie characters, implementing gameplay mechanics, and adding final touches.

Setting Up the Project

  1. Install Unity: First, make sure you have Unity installed on your computer. You can download the latest version from the official Unity website.

  2. Create a New Project: Open Unity Hub, click on "New Project," and select the "2D" template. Name your project (e.g., "ZombieSurvival") and choose a location to save it.

  3. Organize Your Assets: In the Project window, create folders to organize your assets. Common folders include "Sprites," "Scripts," "Scenes," and "Prefabs."

Designing the Game Environment

  1. Create a Scene: In the "Scenes" folder, create a new scene (e.g., "MainScene"). Open the scene by double-clicking on it.

  2. Design the Background: Import your background images into the "Sprites" folder. Drag and drop a background image into the scene. Adjust its position and scale to fit the camera view.

  3. Add Platforms and Obstacles: Use simple sprites or tilemaps to create platforms and obstacles. You can import platform sprites or draw your own. Place these elements in the scene to create the game's layout.

Creating the Player Character

  1. Import Player Sprite: Import your player character sprite into the "Sprites" folder. Drag and drop the sprite into the scene to create a GameObject.

  2. Add Components: Select the player GameObject and add the following components via the Inspector:

    • Sprite Renderer: To display the sprite.
    • Rigidbody2D: To handle physics.
    • Box Collider2D: To detect collisions.
    • Player Script: To control player behavior (we'll create this script next).


  1. Create Player Script
    : In the "Scripts" folder, create a new C# script (e.g., "PlayerController"). Open the script and add the following code:

csharp
using UnityEngine; public class PlayerController : MonoBehaviour { public float moveSpeed = 5f; public float jumpForce = 5f; private Rigidbody2D rb; private bool isGrounded = false; void Start() { rb = GetComponent<Rigidbody2D>(); } void Update() { float moveInput = Input.GetAxis("Horizontal"); rb.velocity = new Vector2(moveInput * moveSpeed, rb.velocity.y); if (Input.GetKeyDown(KeyCode.Space) && isGrounded) { rb.AddForce(Vector2.up * 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; } } }
  1. Assign Script to Player: Attach the "PlayerController" script to the player GameObject in the Inspector.

Creating Zombie Enemies

  1. Import Zombie Sprite: Import your zombie sprite into the "Sprites" folder. Drag and drop the sprite into the scene to create a GameObject.

  2. Add Components: Select the zombie GameObject and add the following components:

    • Sprite Renderer: To display the sprite.
    • Rigidbody2D: To handle physics.
    • Box Collider2D: To detect collisions.
    • Zombie Script: To control zombie behavior (we'll create this script next).
  3. Create Zombie Script: In the "Scripts" folder, create a new C# script (e.g., "ZombieController"). Open the script and add the following code:

csharp
using UnityEngine; public class ZombieController : MonoBehaviour { public float moveSpeed = 2f; private Rigidbody2D rb; void Start() { rb = GetComponent<Rigidbody2D>(); } void Update() { rb.velocity = new Vector2(-moveSpeed, rb.velocity.y); } void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Player")) { Destroy(collision.gameObject); // Implement game over logic here } } }
  1. Assign Script to Zombie: Attach the "ZombieController" script to the zombie GameObject in the Inspector.

Implementing Gameplay Mechanics

  1. Health System: Create a health system for the player. In the "Scripts" folder, create a new script (e.g., "PlayerHealth"). Add the following code:
csharp
using UnityEngine; public class PlayerHealth : MonoBehaviour { public int maxHealth = 3; private int currentHealth; void Start() { currentHealth = maxHealth; } public void TakeDamage(int damage) { currentHealth -= damage; if (currentHealth <= 0) { Destroy(gameObject); // Implement game over logic here } } }
  1. Zombie Damage: Modify the "ZombieController" script to damage the player instead of destroying it instantly:
csharp
void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Player")) { collision.gameObject.GetComponent<PlayerHealth>().TakeDamage(1); } }
  1. Player Attack: Implement a basic attack mechanic for the player. In the "Scripts" folder, create a new script (e.g., "PlayerAttack"). Add the following code:
csharp
using UnityEngine; public class PlayerAttack : MonoBehaviour { public Transform attackPoint; public float attackRange = 0.5f; public LayerMask enemyLayers; public int attackDamage = 1; void Update() { if (Input.GetKeyDown(KeyCode.F)) { Attack(); } } void Attack() { Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers); foreach (Collider2D enemy in hitEnemies) { enemy.GetComponent<ZombieController>().TakeDamage(attackDamage); } } void OnDrawGizmosSelected() { if (attackPoint == null) return; Gizmos.DrawWireSphere(attackPoint.position, attackRange); } }
  1. Zombie Health: Modify the "ZombieController" script to include health and damage methods:
csharp
public int maxHealth = 3; private int currentHealth; void Start() { rb = GetComponent<Rigidbody2D>(); currentHealth = maxHealth; } public void TakeDamage(int damage) { currentHealth -= damage; if (currentHealth <= 0) { Destroy(gameObject); } }

Adding Final Touches

  1. User Interface: Create a basic UI to display the player's health. In the "Canvas," add a "Text" component to show the health value. Update the "PlayerHealth" script to modify the UI:
csharp
using UnityEngine.UI; public Text healthText; void Start() { currentHealth = maxHealth; healthText.text = "Health: " + currentHealth.ToString(); } public void TakeDamage(int damage) { currentHealth -= damage; healthText.text = "Health: " + currentHealth.ToString(); if (currentHealth <= 0) { Destroy(gameObject); // Implement game over logic here } }
  1. Audio: Add sound effects for player actions and zombie interactions. Import audio clips into the "Audio" folder and use the "AudioSource" component to play sounds when certain events occur (e.g., attacking, taking damage).

  2. Polish and Testing: Playtest your game to ensure it runs smoothly and is fun to play. Adjust the mechanics, difficulty, and visuals as needed based on feedback and testing results.

Congratulations! You've created a basic 2D action zombie game in Unity. While this guide covers the fundamental steps, there's always room for further enhancement and complexity. You can add more levels, power-ups, different types of enemies, and more advanced gameplay mechanics to make your game even more engaging and enjoyable. Happy game development!