Towers

Vérifié avec version: 2017.2

-

Difficulté: Intermédiaire

Setting up the project

First we will need to install and open the Unity editor. The Tower defense Template can be downloaded from the Asset Store window.

For the sake of this tutorial, we’ll use a blank scene. The Starter Kit does come with several pre-made stages that you can view and compare your own work to.

Towers

Introduction

The most important element in the tower defense game is the tower. In the Tower Defense Template, there are two key elements that we use to create a tower. The first is the tower component, which defines basic functionality for the tower. The second is the TowerLevel component, which defines how a tower behaves when it is upgraded.

Creating a Tower

Let's create a new tower from scratch.

Before we create the Tower GameObject itself, we must create the data objects that store data about how it behaves at different levels. These objects are called TowerLevelData ScriptableObjects.

We'll begin by creating a TowerLevelData ScriptableObject to store data about the first level of our tower.

  • In the Project window, click on the Create menu at the top right and choose Create > Tower Defense > Tower Configuration

  • Give the ScriptableObject a name made up of its type and its level number (eg. Laser1)

  • Place the ScriptableObject in Data/Towers

  • Set values for tower name, health, and price

Now let's repeat this process to create the data object for the second level of our tower.

  • In the Project window, click on the Create menu at the top right and choose Create > Tower Defense > Tower Configuration

  • Give the ScriptableObject a name made up of its type and its level number (eg. Laser2)

  • Place the ScriptableObject in Data/Towers

  • Set values for tower name, description (shown when upgrading to this level), health, and price

Next, we need to create a TowerLevel Prefab for each of these levels. A TowerLevel Prefab contains a TowerLevel component with a reference to the ScriptableObject associated with that level of the tower. The Prefab will also have child objects that determine how the tower targets and attacks enemies.

Let's create a TowerLevel Prefab for the first level of our tower.

  • In the Hierarchy window, click on the Create menu and choose Create > Create Empty

  • Give the new GameObject a name to indicate its tower type and level (eg. LaserTower_1)

  • Add a TowerLevel component to the GameObject

  • Drag the ScriptableObject for this tower’s level into the Level Data field

  • Add meshes that represent the tower as children of the GameObject

  • Create a folder for the tower in Prefabs/Towers

  • Drag the GameObject from the hierarchy view into the newly created folder to make it a prefab

Now let's repeat this process to create the data object for the second level of our tower.

  • In the Hierarchy window, click on the Create menu and choose Create > Create Empty

  • Give the new GameObject a name to indicate its tower type and level (eg. LaserTower_2)

  • Add a TowerLevel component to the GameObject

  • Drag the ScriptableObject for this tower’s level into the Level Data field

  • Add meshes that represent the tower as children of the GameObject

  • Create a folder for the tower in Prefabs/Towers

  • Drag the GameObject from the hierarchy view into the newly created folder to make it a prefab

Now that we have the TowerLevelData ScriptableObjects and TowerLevel Prefabs we need, we can create the Tower GameObject itself.

  • Open a new Scene

  • Create an empty GameObject

  • Set the GameObject name to "NewTower"

  • Add a Tower component to the NewTower GameObject

The Tower component requires a few other components to function, so we will next add these.

  • Add an appropriate Collider component that encapsulates the tower’s mesh to the NewTower GameObject

A Collider component is necessary for a tower to be able to determine if it was hit by a projectile. The Collider also establishes an area where a player can click on the tower after it has been placed to select it.

  • Add a DamageCollider component to the NewTower GameObject

The DamageCollider component will handle collisions with objects that have Damagers (see Action Game Framework Reference).

Now we can configure the settings on the Tower component on our NewTower GameObject.

  • Drag in the Player SimpleAlignment ScriptableObject into the Alignment field

  • Set the Target Transform if necessary

  • Set the Size field under Levels to the number of TowerLevel objects required

  • Drag the TowerLevel Prefabs for this Tower into the Element fields

  • Name the Tower in the Tower Name field

  • Set the X and Y values under Dimensions to set how much space the Tower requires to be placed on the placement grid

  • Add a Health bar object as a child of the Tower GameObject

The Tower prefab will contain all visual information for that particular level of the tower. The Tower prefab will also contain the tower’s affectors and launchers (described below).

Understanding the Tower component

This Tower component will allow us to set values that are common to all levels of the tower, and to set references to TowerLevel Prefabs that contain data for individual tower levels.

Configuration

Max Health

The maximum health of the tower.

Please note that if a TowerLevel Prefab is assigned for level 1 of the Tower, the value set here will be overridden by the MaxHealth value of that TowerLevel component.

Starting Health

The starting health of the tower.

Please note that if a TowerLevel ScriptableObject is assigned for level 1 of the Tower, the value set here will be overridden by the StartingHealth value of that TowerLevel ScriptableObject .

Alignment

Alignment determines which potential target the tower will fire at. We set this with a reference to a SimpleAlignment ScriptableObject.

The starter kit provides two SimpleAlignment ScriptableObjects:

  • Player - the tower will fire at enemy units.

  • Enemy - will fire at anything with a Player alignment.

A SimpleAlignment ScriptableObject is a simple implementation of how to represent teams for a game. A SimpleAlignment is configured using a list of other SimpleAlignments that it considers enemies and can attack and damage. The Tower defense Template includes the two alignments above, but it is possible to create other custom SimpleAlignments.

To create more alignments, from the Project window choose Create > Starter Kit > Simple Alignment. Alternately, we can implement our own version of the IAlignmentProvider script if we need more complicated logic.

Target Transform

Ordinarily when Towers or Agents target each other, they will aim for the origin of their transforms. The TargetTransform allows us to set an alternate point to fire at.

Levels

Here we can set the number of levels the tower has, and also associate a TowerLevel Prefab (more detail on this below) with the Tower component.

Tower Name

This field allows us to enter a name for the Tower that is displayed to the player.

Tower Description

This field allows us to enter a short description for the Tower that is displayed to the player. We can use this to tell the player about the tower's purpose and abilities.

Dimensions

Here we can set how much space the tower takes up on the placement grid.

Enemy Layer Mask

This determines which physics layers the tower can target.

Understanding the TowerLevelData ScriptableObject

Description

This is a short string that will appear on the UI when a user selects a tower of this type.

Upgrade Description

A short string that informs the user how the tower will change when upgraded. Important to note is that the text is displayed when upgrading to this tower level, so the first level will not require one.

Cost

Cost refers to how much it will cost for the player to buy or upgrade to this level of the tower.

Sell

Sell refers to how much money the player will receive for selling the tower during the game.

Sell is usually lower than Cost, but it doesn’t have to be. Note that in the example project the sell value for higher level towers takes into account the accumulated currency the player has spent on all tower levels.

Max Health

The maximum health of the tower, when it is upgraded to this level.

Starting Health

The starting health of the tower, when it is upgraded to this level.

Icon

The Icon field can be used to assign a sprite to represent the tower in UI. More information on this and how it ties into the list of available towers for each stage is available in the Setting Up a Stage section.

Understanding the TowerLevel component

Tower Ghost Prefab

The preview model shown when a player is placing a tower.

Build Effect Prefab

The particle effect Prefab assigned here will be instantiated whenever the tower is built or upgraded, along with its associated sound effects.

LevelData

The TowerLevelData ScriptableObject that contains the data for this level.