How to create a 2D Roguelike in Unity - Part 2 Step-to-step tutorial guide to create a 2D Roguelike videogame in Unity. Part 2: Enemies, base AI and Combat System

Creare un Roguelike 2D in Unity - Parte 1

Let’s continue our journey in creating a 2D roguelike in Unity. In the first part, we created the project, set up the graphics, and implemented the player movement. Now it’s time to bring the dungeon to life: we’ll add enemies, program a basic AI to chase the player, and implement a simple combat system.

Remember, all the source code is available in the official 2DRogueTest GitHub repository, which we strongly recommend checking to ensure you're viewing the latest version of the code!

1. Adding Enemies

The project already includes the base enemy prefab: /Prefabs/Enemy.prefab. This GameObject includes:

  • a SpriteRenderer with the enemy.png sprite
  • a Rigidbody2D (Body Type: Dynamic)
  • a BoxCollider2D
  • the EnemyController.cs script found in /Scripts/EnemyController.cs

The enemy prefab can be instantiated manually in the scene or via script.


2. Moving Toward the Player (Basic AI)

The EnemyController.cs script contains a simple yet effective logic to chase the player:

This simple AI moves each enemy toward the player at a constant speed. Make sure the player GameObject has the Player tag set.


3. Combat System

To add a health and death system, each entity (player and enemies) must have hit points. The project includes a reusable script called EntityStats.cs in /Scripts/EntityStats.cs:

The PlayerController.cs script has been updated to allow attacking enemies when the Space key is pressed. In the Update() method, you’ll find:

And the TryAttack() method is defined as follows:

This basic area attack hits all enemies within a 0.5 unit radius.

Note:

  • Enemies must have the Enemy tag.
  • The EntityStats script must be added to both the player and the enemies.

4. Damage System for the Player

Enemies can deal damage too. In the EnemyController.cs script, you can add this method:

This way, every time an enemy touches the player, it deals 1 damage.

You can enhance this system with cooldowns between attacks, animations, and visual feedback.


5. Visual Feedback and Polish

The project includes:

  • animations for enemies and player in /Animations
  • placeholder sound effects
  • basic logic to flash enemies when they take damage

All of these are optional but enhance the player experience.


Conclusion

In this second part, we’ve brought the dungeon to life: enemies now chase the player, attack, and can be defeated. Everything is handled with simple and modular scripts, all included in the 2DRogueTest repository.

In the third and final part, we’ll see how to automatically generate rooms, link them together, and make every run unique.

Fork me on GitHub

About Ryan

IT Project Manager, Web Interface Architect and Lead Developer for many high-traffic web sites & services hosted in Italy and Europe. Since 2010 it's also a lead designer for many App and games for Android, iOS and Windows Phone mobile devices for a number of italian companies. Microsoft MVP for Development Technologies since 2018.

View all posts by Ryan

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

This site uses Akismet to reduce spam. Learn how your comment data is processed.