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.
Lessons
- 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
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 :)