Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Creating an RTS Game in Unity 2023

You're reading from   Creating an RTS Game in Unity 2023 A comprehensive guide to creating your own strategy game from scratch using C#

Arrow left icon
Product type Paperback
Published in Oct 2023
Publisher Packt
ISBN-13 9781804613245
Length 548 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Bruno Cicanci Bruno Cicanci
Author Profile Icon Bruno Cicanci
Bruno Cicanci
Arrow right icon
View More author details
Toc

Table of Contents (23) Chapters Close

Preface 1. Part 1: Foundations of RTS Games
2. Chapter 1: Introducing Real-Time Strategy Games FREE CHAPTER 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

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 Occlusion Culling 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 Occlusion Culling, for example, the Unity Engine uses User Layer as an index, and for that reason, it is not recommended to change or move layers once they have been created and used, to avoid unexpected behaviors and issues.

The next setting we are going to update is physics. Besides updating the Layer Collision Matrix, we are also going to change one very special option here that is required to make the collision detection work better with the NavMesh system. Let us set up the new layer in the physics configuration:

  1. Open the Project Settings window and, in the left panel, click on Physics.
  2. Change the Contact Pairs Mode property to Enable All Contact Pairs.
  3. Expand the Layer Collision Matrix property and uncheck the intersections where the Enemy column meets the Enemy, Portrait, and Plane rows.
  4. Save the project.

Each new custom layer that we add will create a new intersection in the Layer Collision Matrix section, making it a good practice to always check the settings in the Physics configuration. The current layer configuration in the following figure is what we have so far, and the goal is to disable the collision between GameObjects within the same Unit layer or in the same Enemy layer:

Figure 10.2 – Updating the Physics settings

Figure 10.2 – Updating the Physics settings

Note that we have previously unchecked all the intersections between the Portrait layer and any other layer in the project because the Portrait layer is only applied to the selected unit shown on the UI and no physics is required there since it is only used for the portrait camera rendering.

Besides the Layer Collision Matrix, there is another Physics setting that we need to change as well, Contact Pairs Mode, which is how the engine manages the collision and trigger events between two GameObjects with Rigidbody components attached. The Default Contact Pairs option, which was the previous default value, will ignore the collision between two kinematic Rigidbody components. However, as we are going to see later, in the Managing the collision and chase behavior section, we are going to enable the kinematic property for our units and enemies; otherwise, the default collision will conflict with the NavMesh by pushing and pulling the GameObjects at the same time as the pathfinder is trying to do so.

To avoid collision detection issues, we changed Contact Pairs Mode to Enable All Contact Pairs – this will make collisions between any combinations of kinematic and non-kinematic GameObjects be detected but without the physics force being applied. The intersection between the Enemy and Unit layers with the Plane layer can also be unchecked because, without physics at play, we do not need collision detection with the ground.

Now that we have created the new Enemy layer, we also need to add it to the Occlusion Culling property of a few cameras to make the GameObjects visible once we set the layer to them:

  1. In the Project view, locate the MiniMapCamera file in the Prefabs | UI path and double-click on it to open the Prefab in Edit Mode.
  2. In the Inspector view, on the Camera component, click on the Occlusion Culling property and select the Enemy layer.
  3. In the Project view, double-click to open the Playground scene and, in the Hierarchy view, click on the GameObject named Main Camera.
  4. With the camera selected, in the Inspector view, on the Camera component, click on the Occlusion Culling property and select the Enemy layer. Save the scene changes.
  5. In the Project view, double-click to open the Level01 scene and repeat the previous step to add the Enemy layer to Main Camera as well.

The MiniMapCamera Prefab is responsible for rendering the map from the top, and, without selecting the Enemy layer, we would not be able to see the enemies in the mini-map. The same applies to Main Camera in each scene – they all must have the Enemy layer in Occlusion Culling to make the GameObject visible to the player.

The following shows all the layers selected in Occlusion Culling for MiniMapCamera.

Figure 10.3 – Adding the Enemy tag to Occlusion Culling

Figure 10.3 – Adding the Enemy tag to Occlusion Culling

The only scene that is not required to have the Enemy layer selected in the camera is GameUI because that specific scene has only the UI layer selected since it is the only thing that should be rendered in this scene. If you have created any other level scenes, remember to select the Enemy layer for Occlusion Culling as well.

Now that we have added the new layer and updated the physics settings, we can move to the battle code, starting with the damage calculation and a few changes to our existing code for both unit and enemy components.

lock icon The rest of the chapter is locked
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 $19.99/month. Cancel anytime
Visually different images