Create New Unit
1. Create Unit Prefab
To create a new unit entity, open the RTS Engine top bar menu and click on New Unit.
This would launch the unit creation in the RTS Engine Editor window.
From this window, you can pick the following initial properties of the new unit:
- Name: This is the name that appears on UI elements, when the unit is selected for example.
- Code: Provide a unique identifier code for the unit. For example: tutorial_unit.
Category: Assign one (or multiple) category for the unit that allows it to belong to certain groups of other entities (which must not only include unit types as categories can be shared between different entity types). To assign multiple categories, make sure to separate them with a comma (',') with no spacing. We will go with: tutorial_unit_category. When you assign a category or set of categories to an entity, you can browse through them and display the other entities that share the same category. As an example, add the military_foot_unit category to this new unit and then select it and you'll be able to see the units that use the latter category (if you have the Demo files imported).
Description: This is the description that appears on UI elements, when the unit is selected for example.
- Icon: To identify the unit visually within the UI elements, we can assign a Sprite asset to it in this field. For example, we can use the Demo archer unit's icon. Search for archer_level1_icon in the Project tab and assign it to this field.
After setting these initial properties, click on the Finalize Creation button on the top right corner of the RTS Engine Editor window.
The new unit prefab is now located under the path: Assets/Resources/Prefabs, which is the default path for newly created entity prefabs. The new entity prefab's name is set after its unique code. If you wish to move the prefab into a different path, make sure that the new path always ends with ../Resources/Prefabs. The latter requirement allows the RTS Engine to cache entity types and make them available for editor components to have a richer user-experience while creating your RTS game with the Unity editor.
Additionally you can cancel the creation of the new entity prefab by clicking on the Cancel Creation button on top right corner of the same editor window.
Next, either double click on the prefab in the Project tab to open it or find it in the RTS Engine Editor entities list in the left side bar (it should be automatically selected after you finalize its creation) and click on Edit in Prefab to open it.
Please notice that the pivot point of the parent game object of the unit is set to the bottom of the object. By default, this is the case where you create a new entity through the RTS Engine menu so make sure this remains unchanged.
We will go through the main components and structure of a RTS Engine unit prefab.
2. Unit Component
The Unit component is the main component of a unit entity, it defines the unit type and its general configurations. This component must be attached to the unit prefab's parent object. Expand this component and let's look at its fields.
2.1 Entity Tab
We will start by looking at the Entity tab which is a tab that holds fields shared by buildings, units and resources.
- Name, Code, Category, Description and Icon fields have been already set in the RTS Engine Editor window when creating the new entity prefab but you can still modify them here.
Model: The unit requires to have a model child object. By default this is assigned to the capsule model. If you have access to the Demo files, search for the archer_model object in the Project tab in order to use one of the demo's unit models. Select it and drag it as a child of the new unit's prefab.
Set the position of the archer_model child object to (0, 0, 0) to center it. Then remove the default unit capsule model and assign the archer_model object to the Model field of the Unit component.
Radius: This field defines the range that the unit entity is supposed to occupy within the map. For unit entities, this value can not be specified directly through the Unit component as it is overridden by the radius set by the movement component of the unit. We will get back to this when we go through the movement setup of the unit.
The Entity tab of the Unit component should look like this now:
2.2 Faction Entity Tab
A unit is a faction entity, meaning that it can belong and be controlled by a faction in the game. That is why it has a dedicated Faction Entity tab in its Unit tab that holds faction-specific fields. This is how this tab looks before we start editing it:
In this tab, we are interested to update the Colored Renderers field while keeping the rest to their default values. If you want to learn about the fields that are not covered within this tutorial then take a look at the Unit inspector document which covers all fields or hover over the fields in the editor and you'll see a tooltip explaining the task of each field. We will also get to introduce some of them in the following tutorials (for example, we will discuss the Init Resources and Disable Resources fields when we introduce resources more thoroughly).
For the Colored Renderers field, it allows us to define what parts of the unit's model that will be colored with the color of the faction that owns the unit. In a previous tutorial, we showed that this color is defined within the Faction Slot in the Game Manager component inspector. By default this field was assigned to the default unit's capsule model and since we have removed that to replace it with the archer_model it is now marked as missing. To fix this, drag and drop the parent object of the archer_model into the Renderer field.
To check what Material ID we need to assign, select the parent object of the model and under its Mesh Renderer component, expand the Materials field. Since we only have one material assigned, its ID would be 0 and that is the value to set on the Material ID of the Colored Renderers field's first element.
Our Faction Entity Tab should look like this now:
2.3 Unit Tab
The last tab of the Unit component is the Unit Tab which includes unit-specific fields. For now, it only includes the Spawn Look At field which determines where the unit will be looking at when it is initialized. We will keep this field unassigned and the unit will be initialized with the rotation it is assigned in its Transform component.
3. Unit Health Component
The Unit Health component is the second required component of a unit that must be attached to the parent object of the unit's prefab. This component handles updating the unit's health, handling its destruction as well as allowing to visually update the unit's objects to highlight its health state to the player.
3.1 General Tab
- Max/Initial Health: Update the unit's maximum health points and the initial health it has. The initial health, the one that the unit is assigned when it is created, must be at least 1.
- Can Increase/Decrease/Be Attacked: We will keep those enabled as we do not want to lock the unit's health update either by increasing or decreasing and we want the unit to be attackable by enemy entities. Some specific use cases might require you to disable one of these options so it completely depends on the behavior you want from the unit.
- Attack Target Position: Drag and drop a child object of the unit whose position would be used as the attack target position when enemy attacker units/buildings target this entity. If not assigned, then the attack target position would default to the child object that has the Unit Selection component attached to it.
3.2 Destruction Tab
- Destroy Object/Delay: We want this option enabled as we want to destroy the unit's entity object when its health reaches 0. The delay option allows you to set time before the object is actually destroyed (during which the unit state would remain as Dead and will not be usable), which allows you to run a destruction animation or effect. In this case, we will keep it simple and leave the delay at 0 seconds so that the unit gets immediately destroyed.
3.3 Health States Tab
We will not be assigning health states for this unit to keep things simple, but check the explanation in the Create New Building tutorial where health states are thoroughly explained.
4. Unit Animation
The third required component on the unit's prefab parent object is the Unit Animator Controller which handles running the animations on the unit depending on its state in the game.
- Animator: A Unity Animator component is required to run animations on the unit. This component can be placed on the unit's parent object, on the unit's model object or any other child object that is required to run the animations you have for the unit. In our case, we will be using animations that require the Animator component to be on the unit's model object. Therefore, make sure that an Animator component is attached there and drag and drop it into the Animator field of the Unit Animator Controller.
Animator Override Controller: Units in the RTS Engine use a unified UnitAnimator animator controller (which can be found under the following path in the Project tab: RTS Engine -> Core -> Animation -> Controllers). This allows all units to have the same pre-defined animator states.
In order to assign different animations to these states, we do not modify the animator controller directly but instead create Animator Controller Overrides for each unit type. Therefore, go right click in the Project tab and choose Create -> Animator Override Controller to create a new one for the tutorial_unit, call it tutoriaL_unit_override_ctlr and select it. Its inspector should look something like this:
In the Controller field, assign the RTS Engine's UnitAnimator controller. That would bring up Animation Clip fields that you can assign for different animator states. By default, these animation clips are empty.
For the tutorial_unit, we will first assign the idle and movement animation clips and keep the rest unassigned. In this case, we can use the unit_idle_animation and unit_walk_animation animation clips from the demo folder. You can just search for them in the project tab and assign them into the override controller.
Now that the animator override controller for the new unit is created, assign it to the Unit Animator Controller component's Animator Override Controller field. The same field allows you to have different override controllers for the animator and from the Fetch Type field, allows you to use one randomly every time the unit is initialized. The latter example is useful when you have multiple animation clips for the same states for a unit and you'd like each created unit instance to have different animations. But in our case, we only have one override controller so the Fetch Type field is irrelevant.
Damage Animation: We will keep the Damage Animation Enabled field disabled because we have not assigned a damage animation clip to the override controller but enabling it would not just make the unit play the damage animation clip but also stay frozen while that animation is played for the duration assigned in the Damage Animation Duration field.
5. Other Main Object Components
The other components that are attached to the unit's entity main prefab object are the following:
- Audio Source: Required when you want the unit components to play audio clips locally on the unit.
- Nav Mesh Agent: Required for the unit pathfinding using Unity's navigation mesh. We will get back to this component in the next tutorial. For now leave the fields unchanged.
That is it for the unit's parent object. Next we will explore some of the essential components in the children objects.
6. Unit Selection
6.1 Unit Selection Component
The unit selection allows us to define what colliders are used to detect the player's mouse click in order to have the unit selected when the game is running.
The main component in the selection process is the Unit Selection component which is placed under the selection child object of the unit prefab.
- Is Active: Make sure this option is enabled so that the unit is selectable.
Selection Colliders: This array field defines the colliders used to select the unit. You can have multiple colliders, placed on multiple child objects of the unit prefab, that allow the selection of a single unit. The latter is useful when you have a complex unit structure which the simple collider types can not cover. Each collider object must have the following:
- Collider component.
- Entity Selection Collider component.
In the case of this tutorial_unit, we have one collider object with a Box Collider of center (0,0.375,0) and size (0.7, 0.9, 0.5) which fits the model we used. Now make sure that this collider object is added to the Selection Colliders field of the Unit Selection component.
6.2 Selection Renderer
We can have a visual representation to indicate when the unit is selected. This is exactly the job of the selection_renderer child object. It includes a Mesh Renderer component which is enabled when the unit is selected and disabled when it is not. This is the task of the Entity Selection Renderer component which is attached to the selection_renderer child object:
- Selection Renderer: Drag and drop the Mesh Renderer component into this field, to be enabled or disabled when the unit is selected or not selected respectively.
- Material ID: The selection renderer is colored using the faction color that the unit belongs to. This field defines the index of the material that will be colored on the Mesh Renderer component. In the case of the tutorial_unit's selection renderer, there is one material and therefore we keep this value set to 0.
The selection renderer seems to be much bigger than the tutorial_unit model. Therefore, we can update the selection_renderer Transform's Scale field to (0.1, 1, 0.1) to make it a better fit.
7. Other Child Object Components
The following child objects and their components are necessary to have a unit prefab but they will be discussed in other tutorials:
- unit_movement: Includes components required to make the unit movable. Discussed in the next tutorial.
- unit_components: An empty object, to be filled by unit-specific components.
Download Files
You can download the results of this tutorial here.