How to Create Enemies in Godot Games
HomeHome > News > How to Create Enemies in Godot Games

How to Create Enemies in Godot Games

Jul 17, 2023

Many games include some kind of foe to battle with. Explore the various ways you can add—and finesse—enemy combat.

Enemies play a crucial role in making games exciting and challenging for players. Whether you're creating a 2D platformer, a top-down shooter, or a role-playing game, adding enemies can significantly enhance the gameplay experience.

Godot's user-friendly interface and intuitive scripting language, GDScript, make the process efficient and enjoyable.

Before diving into enemy creation, set up the basic structure of your 2D game in the Godot game engine.

Create a new 2D project in Godot. In the main scene, create a new KinematicBody2D node and name it Player. Inside the player node, add a CollisionShape2D with a rectangle shape, which will be the player's hitbox. Also add a Sprite node as a visual representation of the player character.

The code used in this article is available in this GitHub repository and is free for you to use under the MIT license.

Attach the following GDScript code to the Player node to enable basic movement:

With this code, the player can move left, right, up, and down using the arrow keys or WASD keys.

Now that you have a player character set up, you can create a simple enemy.

Create a new scene and add a StaticBody2D node named Enemy. Inside the Enemy node, add a CollisionShape2D node with a circle shape to define the enemy's hitbox. Also add a Sprite node to represent the enemy visually.

With the collision set up, the enemy will be ready to interact with the player and other elements in your game world.

Creating enemies that follow the player adds a new level of challenge to the game.

Create a new scene for the following enemy type and add a KinematicBody2D node named FollowEnemy with a CollisionShape2D. Attach the following GDScript code to the FollowEnemy node to make it follow the player:

Now, the FollowEnemy will move toward the player's position in each frame.

Now, you can make the enemy shoot bullets toward the player. Create a new scene and name it Bullet.tscn. Add a KinematicBody2D node to the scene. Attach the Bullet.gd script to the KinematicBody2D node in the Bullet.tscn scene. Add the following code to define the Bullet class:

Now, create a new node for the enemy with bullets and name it ShootingEnemy. Inside the ShootingEnemy node, add a CollisionShape2D node with a rectangle shape as the hitbox.

Attach the following GDScript code to the ShootingEnemy node to make it shoot bullets:

The enemy will now periodically shoot bullets toward the player's position. Use a conditional if statement to check if the shoot_timer is less than 0. If it is, then shoot the bullet.

In addition to enemies that follow or shoot at the player, a randomly moving enemy can inject an element of unpredictability and challenge into your game. Creating an enemy with random movement patterns requires a combination of simple logic and random number generation.

Start by creating a new scene and add a KinematicBody2D node named RandomEnemy. Create a new script named RandomEnemy.gd and attach it to the RandomEnemy node. The script will handle the random movement logic.

There are many additional features you can add to your enemies to make gameplay more interesting.

Boss battles serve as climactic moments in your game, providing memorable and challenging encounters that test players' skills and perseverance. Boss enemies are typically larger and more powerful than regular foes, requiring players to employ specific strategies and tactics to defeat them.

Implement a dynamic spawning system that adjusts enemy encounters based on the player's performance, location, or in-game events. This creates a more responsive and personalized gameplay experience.

Create enemies that can adapt to the environment, such as enemies that can fly, swim, or climb walls. This versatility opens up new gameplay possibilities and challenges players to navigate different terrains.

Assign specific weaknesses and resistances to enemies, encouraging players to experiment with different approaches and tactics. Some enemies may be vulnerable to certain attacks or elements, while others are immune or resistant.

Add a level of unpredictability by giving enemies randomized behavior variations. For example, an enemy might have different attack patterns or movement speeds in different playthroughs, keeping the gameplay fresh and replayable.

Including a variety of these additional features can enrich your game's enemy design, making each encounter unique and memorable for players.

Remember that while introducing new mechanics can be exciting, it's crucial to maintain balance and coherence within your game's overall design.

When creating enemies in your Godot game, consider the following best practices:

Use distinct enemy designs that stand out from the environment to make enemies easily recognizable.

Consider using color-coding or unique silhouettes to differentiate enemy types. Make sure enemy animations and visual effects reinforce their behaviors and attacks.

Gradually introduce enemies with increasing complexity as the player progresses through the game. Test enemy encounters with players of varying skill levels to ensure challenges are suitable for all players. Avoid sudden difficulty spikes that can frustrate players.

Design enemies with attacks that the player can dodge or block, giving them a chance to react. Use a fair hitbox for enemies' attacks, avoiding attacks that hit beyond their visual representation. Use warning signs or cues to hint at dangerous attacks.

Regularly test enemy encounters during development to evaluate their fun factor and difficulty. Gather feedback from players and use it to fine-tune enemy behaviors, strengths, and weaknesses.

Sound effects play a crucial role in creating an immersive and captivating game environment. When an enemy takes a hit, add appropriate copyright-free sound effects to reinforce the sense of impact and engagement in combat.

Your game can accompany each strike or hit with distinct audio cues that correspond to the type of attack and the enemy's characteristics.

Enemies are a fundamental component of many games, providing challenges, obstacles, and a sense of accomplishment when defeated. By adding different types of enemies with varying behaviors, health points, and shooting mechanics, you can create diverse and engaging gameplay experiences for players.

Remember to balance the difficulty, offer visual and auditory feedback, and test your enemies thoroughly to provide players with an enjoyable and rewarding gaming experience.

Imran is a writer at MUO with 3 years of experience in writing technical content. He has also worked with many startups as a full-stack developer. He is passionate about writing and helping others learn about technology. In his free time, he enjoys exploring new programming languages.

KinematicBody2DPlayerCollisionShape2DSpritePlayerStaticBody2DEnemyCollisionShape2DSpriteKinematicBody2DFollowEnemyFollowEnemyBullet.tscnKinematicBody2DBullet.gdShootingEnemyCollisionShape2Dif shoot_timerKinematicBody2DRandomEnemyRandomEnemy.gd