Reader small image

You're reading from  Creating an RTS Game in Unity 2023

Product typeBook
Published inOct 2023
Reading LevelN/a
PublisherPackt
ISBN-139781804613245
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Bruno Cicanci
Bruno Cicanci
author image
Bruno Cicanci

Bruno Cicanci is a software engineer and game developer with professional experience on different technologies and platforms. Since 2009, Bruno helped to develop and publish many games, mainly using Unity for mobile devices. He writes about game development on his personal blog, which led him to present many talks at universities and events. Bruno holds a Computer Science BS. Degree and a specialization and Game Production and Programming. In the last decade, he worked at prestigious game studios such as Glu Mobile, Electronic Arts, and Aquiris. He is currently developing games and reusable technologies at Ubisoft. Originally from Sao Paulo, Brazil, Bruno currently resides in London, UK with his wife and two adorable cats.
Read more about Bruno Cicanci

Right arrow

Adding Enemies

An RTS game has maps, units, and many other things, but there is one game mechanic that cannot be left out, which is combat. However, to have a good and challenging battle system in place, we need more than units; we need to have enemies.

In this chapter, we are going to implement different enemy types, such as Orcs and Golems, against whose attacks it will be a real challenge for the player to defend themself, and the mighty Red Dragon, which we are going use later as the boss of our level.

By the end of this chapter, you will have learned how to create a flexible system for adding different enemies, spawning the enemies in the existing maps, and reusing most of the unit components to expand and create unique features for the enemies. You will also learn how to create damage feedback in the UI that will display the damage done to each unit and enemy, as well as how to calculate the damage considering the configuration.

In this chapter, we will cover the following...

Technical requirements

The project setup in this chapter with the imported assets can be found on GitHub at https://github.com/PacktPublishing/Creating-an-RTS-game-in-Unity-2023 in the Chapter09 folder inside the project.

Configuring the enemies

So far, we have already built a solid foundation for our RTS game, with a flexible UI and controllable units. Now, we are going to start adding all the enemies the player will face in the game and prepare another layer of a solid strategy game. The enemies will be an Orc, fast but a bit weak on its own; a powerful Golem, which is slower than the Orc but much stronger and resistant; and our mighty boss, the Red Dragon, which will be a challenge for the player.

Figure 9.1 – The Orc, Golem, and Red Dragon enemies

Figure 9.1 – The Orc, Golem, and Red Dragon enemies

All the enemies in the preceding figure have their Prefabs located inside packages that were already imported into the project in Chapter 2, Setting Up Unity and the Project, so we have all the Prefabs that we need. However, we will need to tweak their animation states, as we did for both Warrior and Mage. That is because the animation states are set up as a loop through all states, and we want to control each animation...

Spawning the enemies

We built a very solid and flexible code base to spawn more objects in the game, using our Object Pool to manage the instances triggered by our message queue. It all started with the Warrior unit, and then we reused everything we had to configure and add the Mage unit as well. Now, we are also going to reuse both systems, but with a few changes since the enemies are a bit different from the units.

The first scripts that we are going to create are the messages to spawn the enemies, and then, in the next sections, the other scripts for the enemy component and spawner. Each enemy will have its own message; however, they will all share the same parameter to determine the position on the map at which the enemy will be spawned. Since it is a common property there is no need to add it to all enemy messages, instead, we are going to create a base enemy message and then implement it on all enemy messages.

Creating the enemy messages

Create a new folder inside Scripts...

Damage feedback in the UI

In an RTS game, we will have many units and enemies at the same time on the battlefield, and we need good feedback to show the player what is happening, giving the player enough information to think and react fast with a different strategy or update the current one. When selecting the units, the player will be able to see the current Health Points (HP) of each unit, but the enemies are not going to be selectable by the player, which means that we need visual feedback that they are being damaged the units, as well as the damage enacted by the enemies on the units, even when not selected.

We are going to create some simple UI feedback that will be displayed on top of the enemy or unit when they take damage. This UI feedback, which will be text about the amount of damage taken, will be visible for a couple of seconds and fade away eventually. We are going to position the UI feedback on top of the 3D model, but with a small random range so that when multiple...

Summary

Great job reaching the end of this chapter! We now have three different enemies added to and configured in our RTS game. The trio of Orc, Golem, and Red Dragon has different configurations and will present a real challenge to the player.

In this chapter, we used most of the systems that were developed in the book so far, showing that they were not made only for the units but could also be used for enemies as well. We learned how to modify, expand, and adapt existing code to add enemies, and we can even spawn them using the debug menu, which now has more options to help us develop our RTS game.

We also created a very useful and flexible damage feedback system to show the player how much damage the enemies and the units are taking and learned how to calculate the damage based on a few properties from the enemy configuration. The Dragoncraft game is reaching an important milestone now with enemies added into the project, with different configurations that will make that...

Further reading

In this chapter, we did not introduce many new concepts or Unity features since we mostly reused things we already covered in this book. However, it is worth mentioning at least a couple of links to complement what we just saw in this chapter:

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Creating an RTS Game in Unity 2023
Published in: Oct 2023Publisher: PacktISBN-13: 9781804613245
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Bruno Cicanci

Bruno Cicanci is a software engineer and game developer with professional experience on different technologies and platforms. Since 2009, Bruno helped to develop and publish many games, mainly using Unity for mobile devices. He writes about game development on his personal blog, which led him to present many talks at universities and events. Bruno holds a Computer Science BS. Degree and a specialization and Game Production and Programming. In the last decade, he worked at prestigious game studios such as Glu Mobile, Electronic Arts, and Aquiris. He is currently developing games and reusable technologies at Ubisoft. Originally from Sao Paulo, Brazil, Bruno currently resides in London, UK with his wife and two adorable cats.
Read more about Bruno Cicanci