Skip to content
Saarrthi Rana

School project / Fall 2024 and fall 2025

Missile and zombie AI in Unity

Game AI assignments in Unity and C#.

Role
Solo
Team
Two AI assignments
When
Fall 2024 and fall 2025
UnityC#Enemy AI
The start of Missile.cs, my missile steering code, on a dark code card
What it is

Both are built on simple rules for turning, avoiding walls and switching behaviour at set distances.

For my Artificial Intelligence course I made a 2D game where you fire a missile and it chases a moving plane, steering around walls. It can only turn so fast, so it curves toward the plane. For another course I made a zombie that patrols, chases you when you get close, and attacks.

What I did
  1. Wrote the missile. Each frame it turns toward the plane by a limited amount and keeps flying forward.
  2. Made it look ahead with two lines and steer away from walls before it hits them.
  3. Wrote the plane that flies back and forth, with a health bar.
  4. Wrote the game manager, which keeps score and spawns a faster, tougher plane after each kill.
  5. Added the launcher, the menus, the sounds and the explosions.
  6. Made the zombie switch between patrolling, chasing and attacking depending on how close you are.
How it works

Turning speed

The missile has a top turning speed. Each frame it turns toward the target by no more than that, so it flies in an arc.

Avoiding walls

Two lines point forward from the sides of the missile. If one hits a wall, the missile turns away and slides along it.

The zombie

It chases you within 10 units and attacks within 2, but only gives up past 15, so it doesn't keep switching between patrolling and chasing.

Code

Code I wrote

Short parts of my own code from this project. I left out anything the course gave us.

Missile steering

Missile.cs, Unity C#

Turning toward the target and staying off the walls. This file was empty in the starter.
    void Update()
    {
        // ...
        float dt = Time.deltaTime;

        
        Vector3 desired = _target ? (_target.position - transform.position).normalized : _forward;

        
        Vector3 steerDir = BlendWithAvoidance(desired).normalized;

        
        float maxRad = maxAngularSpeedDeg * Mathf.Deg2Rad * dt;
        _forward = Vector3.RotateTowards(_forward, steerDir, maxRad, 1f).normalized;

        
        transform.position += _forward * speed * dt;
        transform.up = _forward;
    }
    // ...
    Vector3 BlendWithAvoidance(Vector3 goalDir)
    {
        
        Vector3 right = new Vector3(-_forward.y, _forward.x, 0f);
        Vector3 left = -right;

        Vector3 oR = transform.position + right * rayOffset;
        Vector3 oL = transform.position + left * rayOffset;

        RaycastHit2D hitR = Physics2D.Raycast(oR, _forward, rayLength, wallMask);
        RaycastHit2D hitL = Physics2D.Raycast(oL, _forward, rayLength, wallMask);
        // ...
        Vector3 blended = goalDir;

        if (hitR.collider || hitL.collider)
        {
            Vector3 n = ChooseCloserNormal(hitR, hitL);
            Vector3 t = new Vector3(-n.y, n.x, 0f);
            if (Vector3.Dot(t, goalDir) < 0f) t = -t;

            blended = goalDir * (1f - normalInfluence) + n * normalInfluence + t * tangentNudge;
            return blended.normalized;
        }
        // ...
    }

Zombie states

Enemy_Zombie.cs, Unity C#

The zombie's three modes. I added this for Lab 5.
    void Update()
    {
        switch (currentState)
        {
            case State.Patrol:
                AvoidObstacles();
                Patrol();
                if (Vector3.Distance(transform.position, player.position) <= chaseDistance)
                {
                    currentState = State.Chase;
                }
                break;

            case State.Chase:
                AvoidObstacles();
                ChasePlayer();
                if (Vector3.Distance(transform.position, player.position) <= stopDistance)
                {
                    currentState = State.Attack;
                }
                else if (Vector3.Distance(transform.position, player.position) > returnDistance)
                {
                    currentState = State.Patrol;
                }
                break;

            case State.Attack:
                Attack();
                if (Vector3.Distance(transform.position, player.position) > stopDistance)
                {
                    currentState = State.Chase;
                }
                break;
        }
    }

The missile, plane and game manager scripts came empty with the assignment. I wrote the code in them.

Next school project

Escape Market

Our first full Unreal game, made with Parth Sharma.