Create Your First Godot 4 Game

Lesson 7: Creating an enemy

Video Notes

Let’s not make our raptor too comfortable! In this lesson, we’ll create an enemy character that patrols platforms.

Corrections:

In Godot 4 Beta 16 or higher, the AnimatedSprite2D’s Playing checkbox has been removed from the Inspector. It has been replaced by the Autoplay button, which can be found in SpriteFrames panel next to the trashcan icon.


It’s possible for an enemy to occasionally spawn underneath the PlayArea and never get activated. This is because the PlayArea is too small compared to the where the platforms can be spawned. This can be fixed by either changing this line in game.gd:

var y = clamp(last_platform_position.y + rng.randi_range(-150, 150), 200, 1000)

to

var y = clamp(last_platform_position.y + rng.randi_range(-150, 150), 200, 600)

or by increasing the size Y of the RectangleShape2D node under the PlayArea’s CollisionShape2D from 1200 to 2000.


  • fusion2k
    Nov. 4, 2022 at 3:35 p.m.

    Small bug in the enemy code, gravity should be: velocity.y += gravity * delta

    In the video the + was missing therefore there is no acceleration due to gravity, just a small floating velocity. Not a big problem since these enemies don’t leave the ground (usually) but it’s fun to push them off the platform at this stage :)

Want to get into the mix? Sign in or register to comment.

Next lesson