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

Tracking Progression and Objectives

Many RTS games have a list of objectives that the player needs to complete to win the level – usually, these objectives need to be completed within a time constraint, forcing the player to think fast and come up with an effective strategy quickly.

In this chapter, we are going to develop the last feature of our RTS game, the game progression, which will use objectives such as collecting resources and defeating enemies before the time is up. We will learn how to create a configuration file to define the objectives, how to track the player progressions on each objective, and how to show the current progression in the UI.

By the end of this chapter, you will learn how to create a flexible and powerful system to configure, track, and display the game objectives and the current player progression. You will also learn how to evaluate whether the player won or lost the game, based on whether they’ve completed the objectives and, if so...

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 Chapter15 folder.

Setting up objectives

In our RTS game, the player will have a list of objectives on each level to complete before running out of time. The objectives are a mix of collecting resources and killing enemies, as well as completing them before the time is up. We are going to create a few scripts and a ScriptableObject to help us configure the objectives and include them in the level so that the player knows what needs to be done in the current level to win the game.

Let’s start with our first script, which will be a base for the following couple of scripts, which will list objectives related to resources and enemies. In the Scripts folder, create a new folder called Objective, and add a new script with the name BaseObjective. Then replace the content with the following code:

using System;
namespace Dragoncraft
{
  [Serializable]
  public class BaseObjective
  {
    public int Quantity;
  }
}

The BaseObjective class...

Tracking objectives

The classes we created so far in this chapter will help us to define the objectives on each level, and now we are going to implement a new class that is going to take care of showing the updated progress to the player, as well as keeping track of the progress.

To track the progress of how many resources the player collected, we will listen to an existing message, the UpdateResourceMessage class, which is triggered when a new number of resources is produced and is already used by our UI to display the values in the top-right corner of the screen.

We are ready to go with the resource collection progress; however, we do not have a similar message that we can use to track how many enemies were killed by the player. So, before moving on to the script that will keep track of the objectives, we first need to add a new message that should be triggered every time an enemy is defeated.

Let’s create a new script in the Scripts | MessageQueue | Messages | Enemy...

Winning or losing the game

We have added a nice new feature to our RTS game, the objectives, and now we can configure the level objectives, track the player’s progress, and display the progress in the UI. However, there is still one thing left to implement here: the condition to win or lose the game with visual feedback to the player.

We are going to create a couple of new scripts with a new message type for the game over condition, a new component to listen to this message, and a new pop-up message that will display the game outcome to the player.

Let’s start by adding the new message type. Create a new script in the Scripts | MessageQueue | Messages | UI folder, name it GameOverMessage, and replace the content with the following code block:

namespace Dragoncraft
{
  public class GameOverMessage : IMessage
  {
    public bool PlayerWin;
  }
}

The GameOverMessage class has only the PlayerWin property, which...

Summary

Well done for reaching the end of this chapter! We have finished developing all the major features of an RTS game and our Dragoncraft game is ready to play, as well as having more levels.

In this chapter, we added the last feature of the book, which was the game progression using objectives. We learned how to set up objectives for time, resource collection, and enemies defeated, and added them into a ScriptableObject that can be used to create different objectives for each game level.

We also implemented a component for tracking the player’s progression in each objective, as well as displaying the status of each objective in the UI to make it easier for the player to track the objectives. We also learned how to pause the game using the timescale, and added this to our fully functional pause popup.

Finally, we learned how to evaluate whether the player won or lost the game and how to trigger a Game Over popup with visual feedback for the player, with a Restart...

Further reading

In this chapter, we were introduced to a few concepts and APIs that are worth finding out more about:

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 €14.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