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

Producing and Gathering Resources

One of the most underrated features of an RTS game is resource gathering and production because, while battle and map exploration are important, only resource management can help the player get stronger than the opponent faster. Some resources are automatically generated, while others require that the player command their units to collect them, making this a crucial part of the game strategy.

In this chapter, we are going to learn how to create a system that will generate resources each second, with an option to generate them automatically or manually, depending on the configuration we are going to have in ScriptableObjects. We will also learn how to implement the automatic generation of Gold resources in our game scenes and the manual generation of Food and Wood resources, which will only happen when the player sends units to a specific GameObject with the Collect command.

By the end of this chapter, you will have learned how to develop and...

Technical requirements

The project setup for this chapter, along with the imported assets, can be found on GitHub at https://github.com/PacktPublishing/Creating-an-RTS-game-in-Unity-2023 in the Chapter13 folder inside the project.

Generating resources

We are almost done developing all the features required for our RTS game, so now, we are going to add resource generation to the game. Resource generation is a feature that will automatically produce one specific resource type, and we will add scripts to make it possible to control how much of a resource is generated per second and a multiplier that will increase resource generation.

So far in our project, we have created a script named ResourceType, which has an enum with the resource types of Gold, Wood, and Food. The usage of the ResourceType enum is in the ResourceUpdater script, which is responsible for receiving a message of the UpdateResourceMessage type and updating each resource amount in the top-right corner of the UI. We also have a debug script, ResourceDebugger, to send the message of the UpdateResourceMessage type and test the UI update.

Now, we are going to add information about the production per second and the production level to each resource...

Gathering resources

The feature for collecting resources manually requires some changes and new configurations regarding the project, Prefabs, scenes, cameras, and scripts. This is because we are adding a new interactable object to the map that the player will be able to send the units to so that they can collect resources and manually generate them while the units are at the resource location.

Adding the new Resource layer

The first thing we are going to add is a new layer. This will be used by the Resource object on the map so that it can collide with the unit, as well as a few other validations that we are going to do when we develop the next script. Open the Project Settings window by going to Edit | Project Settings…, select the Tags and Layers menu from the left panel, and add Resource as the value for User Layer 11. This should look as follows:

Figure 13.4 – The new Resource layer

Figure 13.4 – The new Resource layer

We have one new layer now, but we also need to...

Testing the resources

We already have a script with debug methods that can be used in the Editor area to add or remove resources of each type. Now, we are going to add a few more methods to this script so that we can also upgrade each of the resource types while the game is running in the Editor area.

Open the script located at Scripts | Editor | Debug | ResourceDebugger.cs and add the UpgradeGold method using the following code block:

[MenuItem("Dragoncraft/Debug/Resources/Upgrade Gold", priority = 6)]
private static void UpgradeGold()
{
  MessageQueueManager.Instance.SendMessage(
    new UpgradeResourceMessage { Type = ResourceType.Gold });
}

Here, we are adding an attribute called MenuItem with the Upgrade Gold menu option to the UpgradeGold. Method. This will send a new message of the UpgradeResourceMessage type with ResourceType set to Gold. When the game is running in the Editor area, we can use this menu option to send the upgrade...

Summary

Well done for finishing this chapter! Resource production is one of the most important features of an RTS game, and without it, players would not be able to grow their units and settlement to attack and defend powerful enemies. The automatic and manual production of resources is part of the game strategy and one more feature that the player must learn to use to dominate the game.

In this chapter, we learned how to generate resources using two different methods: automatic resource generation, which was implemented to generate Gold, and manual resource generation, implemented on both Food and Wood. Here, you set up the data required to generate these resources and created a new inventory manager to store the amount of each resource that the player has.

We also implemented a collision-based feature for manual resource generation, where the player must send a unit to collect resources. This only happens when the unit is colliding with the object on the map that generates...

Further reading

In this chapter, we covered a lot of concepts and Unity features that are already familiar since we covered them in other chapters in this book. However, we saw something new, and it is worth reading more about it:

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