Lesson 7: Creating an enemy
Let’s not make our raptor too comfortable! In this lesson, we’ll create an enemy character that patrols platforms.
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
var y = clamp(last_platform_position.y + rng.randi_range(-150, 150), 200, 1000)
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.
- Lesson 1: Getting started with Godot 4
- Lesson 2: Overview of the Godot interface
- Lesson 3: Setting up the level
- Lesson 4: Creating the player
- Lesson 5: Spawning the platforms
- Lesson 6: Creating a collectible
- Lesson 7: Creating an enemy
- Lesson 8: Handling player death
- Lesson 9: Creating the player projectile
- Lesson 10: Adding the final touches
- Lesson 11: Exporting your game and wrapping up