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

Adding Enemies to the Map

Most RTS games have groups of enemies that work together to attack the player units while they are exploring the map covered by the Fog of War, making it difficult to predict where the enemies could be hidden, patrolling an area, and waiting for the right moment to attack from the shadows.

In this chapter, we are going to learn how to create enemy groups, with different configurations, and how to add them to the map using predefined spawn points. We will also learn how to implement a fog to cover all of the map but leaving the initial area clear, as well as how to clear the fog above the units while they are moving to covered areas of the map. To make the enemies even more dynamic, we are going to add a new behavior that will allow them to patrol an area between two points, creating more challenges for the player when exploring the map.

By the end of this chapter, you will learn how to create a flexible solution to add one or multiple enemies in a group...

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

Creating spawn points

In Chapter 9, Adding Enemies, we learned how to configure different types of enemies and how to spawn them in a fixed position using the Debug menu. Later, in Chapter 10, Creating an AI to Attack the Player, we learned how to set up the physics and NavMesh systems, creating attack and chase behaviors against the units. Now, we are going to use everything we have developed for the enemies so far to set up spawn points, which are configurable locations on the map where we can spawn one or more enemies when the player starts to play the level.

We are going to create a new configuration file using ScriptableObject to define a group of enemies that are going to be spawned in one specific position. Create a new script in the Scripts | Enemy folder, name it EnemyGroupData, and replace the content with the following:

using System.Collections.Generic;
using UnityEngine;
namespace Dragoncraft
{
  [CreateAssetMenu(
    menuName = "...

Adding fog on the map

Fog of War is a term used in strategy games that indicates that part of the map is hidden from the player, and it is only revealed once the player sends units to explore the covered map. There are some variations. For example, the fog could reappear when no unit is in the explored area, but for our RTS game, we are going to use a more classic approach where any area that is explored stays without the fog covering the map.

Adding a new layer and updating cameras

We are going to need a few things to set up the fog, which includes creating a new layer, a new material, a new mesh, and a new script that will manipulate the material color using Raycast and update a couple of existing scripts in the project. Let us start with the new layer that will be used to render the fog:

  1. Open the Project Settings window using the Edit | Project Settings… menu option.
  2. On the left panel, click on Tags and Layers, and, on the right panel, expand the Layers...

Patrolling the area

Having different enemy groups on the map and hidden under the fog is already quite a challenge for the player. However, finding idle enemies while exploring the map is not very exciting. We can make it more fun and dynamic by adding patrol behavior to the enemies, so they are moving around between two points on the map.

Since we are using Unity’s NavMesh system, it is very simple to modify the existing Enemy ComponentNavMesh script and add the patrol behavior, so the enemy will be able to walk around the area, attack a unit when a collision is detected, and chase the unit if it tries to escape.

Open the existing script located at Scripts | Enemy | EnemyComponentNavMesh.cs and add the following variables inside the class, before the declaration of the existing Start method:

private int _currentPoint;
private Vector3 _startPosition;
private Vector3[] _points = new Vector3[2] {
  new Vector3(-10, 0, 0), new Vector3(10, 0, 0) };

The three...

Summary

This was a very fun chapter – well done for finishing it! We added more features that are fundamental to RTS games, and we have almost reached the point where we have a solid gameplay experience with many challenges for the player.

In this chapter, we learned how to create and set up groups of enemies, and how to add them to the map using configurable spawn points. An enemy group is a very powerful and flexible solution to rapidly add or reuse enemies on the map, and we are going to use it extensively as we progress toward the end of the book.

We also learned how to add the Fog of War, a must-have feature present in any RTS game that has a map covered by fog to make the player explore areas by sending units. Our fog nicely clears once the player moves the camera and the selected units to any area of the map, revealing enemies hidden and waiting to attack the units. Our enemies have gotten a bit smart and challenging now that we have learned how to implement the...

Further reading

In this chapter, we saw a few new APIs and concepts from Unity. The following links will help you to understand the topics we covered in this chapter better, as well as helping you with doubts you might have regarding this content:

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}