Enemy on Platform

A basic wall-bouncing enemy will walk off platform edges and fall. A platform-aware enemy checks for ground ahead before stepping forward and turns back if there is none. This tutorial adds that edge-detection logic to enemy movement.

Loading editor…

Random Direction Changes

A turnChance percentage applied each frame creates variation between enemy types without adding new AI logic:

const ENEMY_PERSONALITIES = {
    PREDICTABLE: { turnChance: 0 },   // Never turns randomly
    CAUTIOUS:    { turnChance: 3 },   // Rarely changes direction
    NORMAL:      { turnChance: 8 },   // Moderate unpredictability
    ERRATIC:     { turnChance: 20 },  // Frequently changes direction
};

// In the main AI loop:
if (!shouldTurn && Math.random() * 100 < enemy.turnChance) {
    shouldTurn = true;
}

The chooseNewDirection function (shown in the demo above) handles edge-detecting enemies differently from free-moving ones: edge-detecting enemies stay horizontal and simply reverse, while free-moving enemies can alternate between axes.

Next: Shoot Him