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 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 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