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

Commanding an Army of Units

One of the most exciting mechanics of an RTS game is preparing the strategy like a chess game and trying to picture what the opponent will do next. Preparing such a strategy involves carefully selecting and moving our units to the best possible places, or even splitting the army to cover different locations on the map.

So far, we have implemented the unit spawning feature, but now we are going to use it to select and move the units on the map. A new Debug menu shortcut will make it easier to instantiate more units in the Unity Editor to test the new features of this chapter.

This chapter will introduce the Physics API and Raycast to select the units on the map, and some vector manipulations to move the units and organize them into a nice grid formation. We are also going to expand some scripts and Prefabs created in previous chapters and have our first contact with materials and the default Unity Shader.

By the end of this chapter, you will know...

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

Preparing the Prefabs and UI

In Chapter 5, Spawning an Army of Units, we were able to instantiate many units using the Debug menu we created. However, the units were spawned at the same place, and we were not able to do anything else with them. Now, we are going to develop a new script that will make it possible to select the units, which is fundamental to giving them an action to perform, such as moving or attacking a target.

We are going to add a few new debugging options to our game to make it possible to enable or disable the camera movement with the mouse, as well as add shortcuts to spawn new units. Then, we are going to start preparing the UI by adding a few new GameObjects to our LevelManager Prefab, and also create visual feedback in the UI to help the player see what is being selected using the mouse on the map.

Adding more debug options

Before creating the script responsible for selecting the units, we will need to make some adjustments to the project. The first...

Selecting the units

There is one more change we need to apply to the project, but this time, we are going to edit an asset from the ThirdParty folder. You probably noticed that both the warrior spawned and the wizard in the UI have an animation that cycles through different states continuously. This is because the Animator of both 3D models was set up like that to showcase the possible animation states. We want to have better control over the animations, so now we are going to remove the transitions between the animation states:

  1. Open the Animator view by clicking on Window | Animation | Animator.
  2. In the Project view, search for FootmanHP and click on the Prefab, you will notice that the Animator view will display the animation states attached to this model.
  3. In the Animator view, click on the white arrows connecting the states and press the Delete key on your keyboard to remove that connection. Do that for all white arrows and save the changes by pressing Ctrl + S (Command...

Moving the units

Now that we have all the unit selection logic in place, we only need a couple more changes in the UnitSelectorComponent class to have it ready to move the units where we want on the map. The Update method, which is already defined in the UnitSelectorComponent class, needs one more validation that we are going to add at the end of the method, as we can see in the following code block, which will be used to check whether the right mouse button was released, using KeyCode.Mouse1:

private void Update()
{
  …
  if (Input.GetKeyUp(KeyCode.Mouse1))
  {
    Vector3 movePosition = GetMousePosition();
    MoveSelectedUnits(movePosition);
  }
}

Once we have the desired move position, which is where the player clicked on the map using the right mouse button, we can call the MoveSelectedUnits method, which is defined next, as well as a new class variable named distanceBetweenUnits:

private...

Summary

Congratulations on reaching the end of this chapter; our project is starting to become a game and soon we will have Dragoncraft as a playable RTS experience. Being able to select and move the units to a specific place on the map is one of the core mechanics of an RTS game and an important part of the strategy when playing this type of game.

In this chapter, we learned how to interact with both the UI and the 3D world using the mouse buttons and position. We also saw how to change the Material Shader property to highlight a model, how to select a 3D object using Raycast and the Physics API, and the logic to place all the units into a grid formation as they move together to a new position.

This chapter was all about the mechanics of selecting and moving the units, with visual feedback to the player and decoupled scripts that isolate the features from each other. We are just starting to implement the unit actions, and soon we will have all the core mechanics working in our...

Further reading

In this chapter, we saw quite a few new APIs from Unity that we might use again in later chapters. The following list includes resources for some of them, including more examples and use cases:

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