2D Platformer – Enemy AI System

2D Platformer – Enemy AI System

An AI-driven enemy system featuring chase, jump logic, combat interaction, health, and loot drops.

2 min read
Published September 5, 2025
Unity
C#
Game Dev
2D Platformer
AI

Overview

The Enemy AI System introduces dynamic challenges to the player.
Enemies patrol, chase, and jump toward the player using raycasts to detect the environment and react intelligently.
They deal damage on contact, take damage from the player’s attacks, and drop loot items when defeated.

The system includes health handling, visual feedback (sprite flashing on damage), and configurable loot tables with rarity percentages.
This makes enemies feel alive, responsive, and rewarding to defeat.


Development & Behavior

  • Chasing: Enemies detect the player’s horizontal position and move toward them using directional velocity.
  • Jumping: Raycasts check for obstacles, gaps, and platforms above. If blocked, enemies jump toward the player with calculated force.
  • Ground Detection: Physics2D.Raycast ensures enemies only chase and jump when grounded.
  • Damage System:
    • Player contact inflicts damage.
    • Enemies flash white briefly when hit, then return to their original color.
  • Health: Configurable maxHealth and currentHealth ensure scalable difficulty.
  • Loot Drop: Randomized loot table determines if gems, health, or boosts drop upon death.
  • Sound & Feedback: Integrated with SoundEffectManager to trigger hit sounds.

Visual Evolution — Before vs After

Early Prototype

The early enemy used placeholder sprites with minimal animation. Movement was rigid, and there was little feedback when taking damage. Loot drop visuals were basic and lacked variation.

Old Enemy Screenshot

Polished Build

The final version features stylized pixel art enemies, smooth chase and jump behaviors, flashing feedback when damaged, and visually distinct loot drops.
Combined with refined animations, they feel integrated into the polished world.

Final Enemy Screenshot

Enemy Sprites & Animations

Idle / Patrol

When idle or patrolling, enemies keep subtle animations — blinking or shifting — to feel alive.

Enemy Idle Sprite

Chase Animation

The chase animation syncs with horizontal velocity. Dust effects are added for urgency as they run toward the player.

Enemy Run Sprite

Future Scope

  • Smarter AI with state machines (patrol, chase, retreat).
  • Ranged enemies with projectiles.
  • Boss enemies with unique patterns.
  • More varied loot tables (rare drops, power-ups).
  • Animation blending for smoother transitions.

Play Demo

Play Enemy Showcase (prototype link)