Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Creating an RTS Game in Unity 2023

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

Product type Book
Published in Oct 2023
Publisher Packt
ISBN-13 9781804613245
Pages 548 pages
Edition 1st Edition
Languages
Author (1):
Bruno Cicanci Bruno Cicanci
Profile icon Bruno Cicanci

Table of Contents (23) Chapters

Preface 1. Part 1: Foundations of RTS Games
2. Chapter 1: Introducing Real-Time Strategy Games 3. Chapter 2: Setting Up Unity and the Dragoncraft Project 4. Chapter 3: Getting Started with Our Level Design 5. Chapter 4: Creating the User Interface and HUD 6. Part 2: The Combat Units
7. Chapter 5: Spawning an Army of Units 8. Chapter 6: Commanding an Army of Units 9. Chapter 7: Attacking and Defending Units 10. Chapter 8: Implementing the Pathfinder 11. Part 3: The Battlefield
12. Chapter 9: Adding Enemies 13. Chapter 10: Creating an AI to Attack the Player 14. Chapter 11: Adding Enemies to the Map 15. Part 4: The Gameplay
16. Chapter 12: Balancing the Game’s Difficulty 17. Chapter 13: Producing and Gathering Resources 18. Chapter 14: Crafting Buildings and Defense Towers 19. Chapter 15: Tracking Progression and Objectives 20. Chapter 16: Exporting and Expanding Your Game 21. Index 22. Other Books You May Enjoy

Creating an AI to Attack the Player

In an RTS game, the battle is one of the most important and challenging features of the game, and it could be the one thing that will make players love or hate the game. Combat must be a fun challenge for the player, and all the actions and decisions must be made by the player during the battle.

In this chapter, we are going to expand the components we have created so far for both units and enemies, adding more physics settings and a new collision component that will work with the NavMesh system to create a solid combat experience for the player. We will also refactor some duplicated code to make it easier for units and enemies to interact with all the new and existing systems.

By the end of this chapter, you will have learned how to set up a solid but flexible physics and collision system, and how it works with a NavMesh system at the same time. You will see how we calculate damage and make the enemy chase the units that are trying to flee...

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 Chapter10 folder inside the project.

Updating the physics settings

In the previous chapter, we included all the variations of enemies in the project and created a couple of scripts to spawn and update them. Now, we are going to expand enemy support by adding a custom layer for them and setting up the layer collision matrix and camera’s Culling Mask to use the new layer.

Let's start by creating the new layer:

  1. Click Edit | Project Settings….
  2. In the Project Settings window, select the Tags and Layers option from the left panel.
  3. Add the Enemy value to User Layer 9.
  4. Save the project.

The following figure shows the current custom layers that we have added so far to our project:

Figure 10.1 – Adding Enemy to the Layers settings

Figure 10.1 – Adding Enemy to the Layers settings

It is important that the Portrait, Unit, Plane, and Enemy custom layers are added to the corresponding User Layer. When setting a layer to a Culling Mask, for example, the Unity Engine uses User Layer as an index, and...

Calculating the damage

Previously, we included all the enemy variations and reused most of the systems created for the units. However, as the project evolves, we have a new need to create a common class for both enemies and units to make it easier for the battle and collision scripts to handle both types of characters. It is a common practice to rework existing code to expand its functionality or improve its overall quality, which is called code refactoring.

We already have some classes used by units and enemies that, even though they are very specific, still share some properties and methods that are the same for both characters. Having a class with common code for both units and enemies will reduce the amount of duplicated code and improve the code usability. The updated versions of the data and component classes will be inherited from the common class for code reuse and will have only the implementation that is different and unique for each character.

Let us start with the...

Managing the collision and chase behavior

Over the few last chapters, we learned how to build a great foundation for an RTS game. In Chapter 7, Attacking and Defending Units, we started to develop the UnitComponent script, which has now been updated to have more features. After that, in Chapter 8, Implementing the Pathfinder, we used the Unity built-in NavMesh system to move units on the map. Then, in Chapter 9, Adding Enemies, we created the EnemyComponent script, which we have also updated now, and added enemy variations.

In this chapter, we updated and expanded the scripts from the chapters mentioned, and now we are going to put everything together to set up combat between units and enemies. First, we will need a new script to handle the collision between units and enemies, and vice versa. Once we have the collision detection ready to use, we are going to update and create the NavMesh scripts so that both systems can work together without causing issues.

Creating the collision...

Implementing the character life cycle

We have everything we need to create an epic battle between units and enemies; however, we need to add one last component to make it possible to reuse them once they are dead in combat. We need to return the GameObjects to the Object Pool, but also clean up all the scripts that we attached to it so that the next time we spawn a new GameObject, there is nothing left from the previous data – it is still the same GameObject instance reused, but with fresh scripts.

We are going to add a new component that will be attached to the GameObject when it is dead. The new component will take care of waiting a few seconds before adding the GameObject back to the Object Pool and removing all the scripts attached since it was spawned. So, create a new script in the Scripts | Battle folder and name it DeadComponent. Then, replace the content with the following code:

using UnityEngine;
using UnityEngine.AI;
namespace Dragoncraft
{
  public...

Summary

Well done on reaching the end of this chapter – it was one of the most challenging so far with many new features and concepts introduced, and a lot of code written. Our RTS game is getting bigger and full of great solid features that we are starting to put together to interact with each other. We are getting so close to having challenging and fun gameplay with many mechanics implemented.

In this chapter, we not only used systems developed in the previous chapters but also refactored and expanded the main scripts used by enemies and units, and, finally, we have a first version of the battle between them. Units can attack enemies with a melee or ranged attack, and, if the player chooses to flee, the enemies will chase down the units until one of the sides is defeated.

We also learned how to set up the physics settings in Unity and how to create a collision script that works nicely with the NavMesh system. Many new concepts and resources were introduced, such as the...

Further reading

In this chapter, we covered a lot of new concepts and features in C# and Unity. The following links will help you to dig a bit deeper into the main topics we have learned:

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 2023 Publisher: Packt ISBN-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.
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 AU $19.99/month. Cancel anytime}