Tutorial: Scripting Course #2 – Let's Create an Enemy

STEP 2: Create the Script for the Enemy (Zombie)


  1. Click the :scroll: + icon to create a new Visual Script.
  2. Rename the first state to Walk, and set the animation to the zombie’s walk animation: E_01_act_003.
    Also, set the Facing Direction to Left, since the default direction is right.

  1. For Walk’s Executable Action, add TemplateMove.
    Leave the template type as Horizontal, but disable Auto Flip so the zombie only turns around when hitting a wall, not based on time.

  1. Let’s test it in-game. Place sample_zombie.tscn as a child of the BaseLayer.
  2. Press F5 or click the :play_button: to test play.
    The zombie should walk left and disappear when hit by a bullet.If it doesn’t work, check the following:
  • Zombie moves in the wrong direction: Make sure “Enable Auto H Flip” is enabled in BaseSettings and the Walk state’s direction is set to Left.
  • Bullets don’t disappear when hitting the zombie: Check the bullet’s script to ensure collision detection is working correctly.
  • The zombie not dying after hits is expected, because we haven’t added the death state yet.

  1. Now let’s create the Damaged state.
    Create a new state near AnyState called Damaged.
    Set the animation to E_01_act_008 (damage animation).

  1. In the action settings, reduce horizontal movement speed to 10%.
    This makes the zombie slow down during the damage animation.

  1. Add three actions:
  2. Play Audio: Use Zombie2 as the sound effect.
  3. Wait: Insert a 0.2s wait to allow the animation to play.
  4. Transfer to Previous Action: Returns to the prior state.

  1. Connect AnyState → Damaged.
    AnyState allows transition from any state, and “Transfer to Previous Action” returns to the prior state—like a “Common Event” for RPG Maker users.

  1. Add two conditions to the link:
  2. Contact With Attack Area
    Configure it to react when hit from any direction by the Player.
  3. HP ≠ 0

  1. Test it. The zombie should groan and stagger when hit, and stop reacting after 5 hits.

If it doesn’t work:

  • No reaction to hits: Check that the HP condition is reversed and the animation is properly set.
  • Still reacts after 5 hits: Make sure the logic condition is AND.

  1. Now let’s make the Death state:

  1. Add three actions to Dead:
  2. Change Object Property
  • Type: Object Property
  • Target: Self
  • Node: AttackCollision (:warning: NOT AttackArea2D)
  • Property: disabled
  • Value: On
    → Disables the hitbox so players don’t take damage from a corpse.
  1. Wait: 0.85s to allow the animation to play.
  2. Remove Self
    → Deletes the zombie from the game.

  1. Link AnyState → Dead, and set the condition to HP = 0 (this time, do not reverse the condition).

  1. Test play again. The zombie should groan and stagger up to 5 times, and on the 5th hit, play the death animation and vanish.

If anything doesn’t work as expected, check the links, states, and conditions carefully.


Next tutorial (Part 3):

2 Likes