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

Crafting Buildings and Defense Towers

RTS games have many different features, and we have added most of the main functionalities to our Dragoncraft game through the book. Now, we are going to develop some new features that are a must-have for RTS games, which will allow the player to craft buildings and the Defense Tower.

In this chapter, we are going to learn how to create a customizable and flexible store for the player to purchase upgrades for resources and units, as well as how to craft buildings that can be moved around the map by the player. We will also learn how to implement the Defense Tower, a powerful unit that attacks enemies from a distance and, as a building, can be used to protect the settlement against invasions.

By the end of this chapter, you will learn how to create a flexible solution to add buildings to the map, increase resource production by upgrading the resource type, and increase the units’ attack and defense attributes with upgrades that will...

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

Crafting and upgrading buildings

Resource management is a big part of RTS gameplay, and the player must think about the strategy when commanding units to gather and expend resources to craft or upgrade new buildings. Crafting and upgrading buildings are the last features we are going to develop before putting together everything we have created so far, in Chapter 15, Game Progression and Objectives, then our RTS game, Dragoncraft, will be ready to play.

We will create new scripts and Prefabs to have a new store pop-up window in the UI so the player can choose which building or unit they want to craft or upgrade. Before creating the scripts for the store popup, let us create another script that is going to be used by all buildings after they are crafted, namely, the drag-and-drop functionality, so the player can position the buildings where they want on the map.

Dragging and dropping buildings

We are going to create a new script that will add the drag-and-drop feature to any...

Training units

We have already done a lot of work to implement our Resource Store. As we create the Unit Store, we are going to reuse everything we have built, so it will be a bit easier and faster to have the Unit Store pop-up window configured and working.

Let us first start with the new scripts that are specific to the Unit Store configuration and then move on to the changes on the existing scripts and the UI. The upgrades on the Unit Store will be how we train the units to become stronger on the battlefield.

Configuring the Unit Store

The first script we are going to create is the message type that we will send when the unit has the upgrade level increased by the player. In the Project view, open the Scripts | MessageQueue | Messages | UI folder, and add a new script named UpgradeUnitMessage. Replace the content with the following code block:

namespace Dragoncraft
{
  public class UpgradeUnitMessage : IMessage
  {
    public UnitType...

Defending with towers

The Defense Tower is a powerful unit that is placed as a building but can attack enemies that are within its range. This is great when defending the base while sending units to explore the map and is a must-have feature for any RTS game.

We are going to reuse many different scripts and GameObjects to add the Defense Tower as a new unit. This section also works as a guide in case you want to add different units to the game later.

Adding the Tower model

We need to import a new asset package from the Unity Asset Store to include a Tower 3D model and Prefab in our project. Go through the following instructions to add the Canon Tower package to your assets and view it in Package Manager so you can import it into the project:

  1. Open the link https://assetstore.unity.com/lists/creating-a-rts-game-5773122416647 (you might need to log in to your Unity Asset Store account created in Chapter 2).
  2. In the list of assets, find the one called Canon Tower and...

Summary

Well done on reaching the end of this chapter! It was a very long chapter, with lots of content and new features, and now we have finally added the last features to our RTS game. We will soon have our Dragoncraft game completed and ready to play.

In this chapter, we learned how to implement a drag-and-drop feature that can be used to move GameObjects on the map, and we applied it to buildings and the Defense Tower. We also learned how to create a customizable and flexible store to list resource upgrades, which also add new buildings on the map as well as increasing the amount of each resource that is generated.

We also implemented the unit upgrade store, reusing everything we created for the Resource Store, and adding specific scripts to upgrade the unit levels, based on the unit type.

Besides the craft and upgrade buildings to generate more resources over time, and the increase in the upgrade level of the training units, we also added a new unit type that works both...

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 £13.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