Suryansh Singh

Dec 04, 2024 • 4 min read • 

How Learning Game Development Made Me a Better Programmer

Lessons from Building a Zombie Survival Game in Unity

How Learning Game Development Made Me a Better Programmer

The Game That Started It All: Welcome to ZombaLand

Let’s kick things off with Welcome to ZombaLand, a first-person zombie survival game built using Unity. In the game, you’re trapped on an island overrun by zombies, and your only mission is to survive. Armed with an array of weapons and a flickering flashlight, you battle waves of undead, trying to escape. This project became my ultimate coding playground, pushing my understanding of programming fundamentals—especially object-oriented programming (OOP), AI, and system optimization.

OOP in Action: Enemies, Humans, and Health Systems

Unity is heavily OOP-centric, and ZombaLand taught me how to structure code using these principles. For example, both the zombies (enemies) and the player (human) shared common attributes, like health, but handled them differently. Here’s a simplified snippet to show how inheritance and abstract classes worked:

// Abstract class for common properties and methods
public abstract class Character {
    public float health;

    public abstract void TakeDamage(float damage);
}

// Player inherits Character and has unique behavior
public class Player : Character {
    public override void TakeDamage(float damage) {
        health -= damage;
        Debug.Log("Player took damage: " + damage);
    }

    public void Heal(float amount) {
        health += amount;
        Debug.Log("Player healed: " + amount);
    }
}

// Zombie inherits Character but handles health differently
public class Zombie : Character {
    public override void TakeDamage(float damage) {
        health -= damage;
        if (health <= 0) {
            Die();
        } else {
            Debug.Log("Zombie took damage: " + damage);
        }
    }

    private void Die() {
        Debug.Log("Zombie is dead.");
        // Zombie death logic
    }
}

In this case, both the player and zombies inherit from a common Character class, but the way they handle damage and health is different. The player can heal, while zombies only take damage and die when their health drops to zero.

AI Programming: Smarter Zombies, Better Systems

One of the most challenging (and fun) aspects of ZombaLand was designing the zombie AI. The goal was simple: make the zombies react to the player in a believable way. But getting there? Not so simple. The zombies had two primary modes of behavior:

  1. Chase the player when they entered a certain range.

  2. React to gunfire and chase the player even if out of range.

I used Unity’s built-in NavMesh for pathfinding. This allowed the zombies to move around the terrain and avoid obstacles. Here’s a snippet of how the zombie’s AI logic was structured:

public class ZombieAI : MonoBehaviour {
    public Transform player;
    public float detectionRange = 20f;
    public float chaseSpeed = 3f;
    private NavMeshAgent navAgent;

    void Start() {
        navAgent = GetComponent<NavMeshAgent>();
    }

    void Update() {
        float distanceToPlayer = Vector3.Distance(player.position, transform.position);

        // Chase if within range or shot
        if (distanceToPlayer < detectionRange || playerHasShot) {
            ChasePlayer();
        }
    }

    void ChasePlayer() {
        navAgent.SetDestination(player.position);
        navAgent.speed = chaseSpeed;
        Debug.Log("Zombie chasing player");
    }
}

With this AI, the zombies would either patrol the map or stand still until the player got too close. Once in range, the NavMesh agent would handle navigation, ensuring the zombies could move through the environment without getting stuck on rocks or trees. The extra detail came with differentiating terrain types—zombies could move faster on flat terrain but slow down on rocky or uneven surfaces. This made the gameplay more immersive and added an extra challenge for the player.

Different Guns, Different Mechanics

Weapon systems were another area where I implemented OOP. Each gun had its own properties—rate of fire, damage, ammo capacity, etc. But they all shared a common Gun class. This allowed me to easily add new weapons without rewriting the core logic every time.

public abstract class Gun {
    public float damage;
    public float fireRate;
    public int ammoCapacity;

    public abstract void Shoot();
}

public class Pistol : Gun {
    public override void Shoot() {
        Debug.Log("Pistol fired, dealing " + damage + " damage.");
        // Pistol shooting logic
    }
}

public class Shotgun : Gun {
    public override void Shoot() {
        Debug.Log("Shotgun fired, dealing " + damage + " damage.");
        // Shotgun shooting logic, multiple projectiles
    }
}

This modular approach made it easy to tweak individual gun behavior, like making the shotgun fire multiple pellets or giving the pistol a faster rate of fire. Once again, this same design pattern applies in web and mobile apps, where you often have common components or services that can be extended for specific use cases.

Raw Programming Skills: Debugging and Optimization

Building ZombaLand was as much about learning the raw skills of programming as it was about the creative process of making a game. I had to optimize everything, from how zombies spawned to how the environment rendered. Memory management, garbage collection, and frame rate optimization became second nature.

Debugging in real-time is one of the toughest challenges. If a zombie stopped moving, I had to dig through the AI code to figure out why. If the frame rate dropped, I needed to profile the game and find what was causing the bottleneck. These are the kinds of skills that have carried over into web and mobile app development, where performance is just as critical—if not more so.

The bottom line...

Game development, especially in Unity, gives you a crash course in programming fundamentals that can be applied across many areas. It teaches you how to manage real-time systems, debug complex interactions, and optimize for performance—all under the pressure of delivering a fun, immersive experience.

References

Join Suryansh on Peerlist!

Join amazing folks like Suryansh and thousands of other people in tech.

Create Profile

Join with Suryansh’s personal invite link.

0

2

0