At this point, you have a game scene in which enemies travel along a path and when it reaches the end simply stops. For a player, this is not very exciting and they have little (or no) incentive to try to stop the enemies. In this section, you will add a Tower to your scene that you player will defend from enemies. If enough enemies reach the Tower, the tower will be destroyed.
model
When you have finished you should have a Prefab that looks similar to the image below:
Just as you did before with the Projectile Test and the TurretAttack Test, it is easier to test your new Tower component in isolation to know that it works the way you would like before adding it to your main level.
In this scene you will create a Tower that can withstand 5 points of damage. You will add two enemies to the scene. One enemy will deal 2 damage. The other will deal 3. After both enemies have damaged the tower, it will be destroyed.
If all went well, you should have a scene that looks and acts similar to the one in the video below:
Before you can damage the tower, your Enemy
will need to know how much damage it deals.
EnemyAttack
int Damage
propertyEnemyAttack
component to your Enemy
prefabDamage
property to 2Damage
property to 3When you have finished, your Hierarchy and Inspector should look similar to the video below:
To be able to detect when an Enemy has collided with your Tower, you will need to add a Collider. I recommend using a Capsule Collider but, depending on the shape of your Tower, you may choose to use a different Collider.
When you have finished, your Tower Prefab should look similar to the one in the video below:
Recall how you implemented the AreaOfEngagement
MonoBehaviour to detect when an enemy moves in range of your turret in 10 - Rotating Turret.
TowerCollisionEvents
UnityEvent<EnemyAttack> OnEnemyHit
void OnTriggerEnter(Collider other)
OnTriggerEnter
, useother.GetComponentInParent<EnemyAttack>()
to get the EnemyAttack
component.OnEnemyHit.Invoke
to notify listeners of the collisionDebug.Log(other)
to your method so you can verify it worksWhen you have finished, your prefab and test scene should look and act similar to the video below.
TowerController
MonoBehaviourfloat BaseHealth
property (set the default value to 5)float Damage
property (set the default value to 0)ApplyHit(EnemyAttack attack)
method:Damage
by attack.Damage
attack.gameObject
this.gameObject
TowerController
component to your Tower PrefabIf all went well, your Prefab and your Test Scene should look and act similar to the video below:
TowerController
MonoBehaviourUnityEvent<TowerController> OnDestroyed
propertyTowerController.ApplyHit
OnDestroyed.Invoke(this)
to notify any listeners that the tower was destroyedGameObject.SetActive
method on your TowerController.OnDestroyed event to enable the LabelIf all went well, your Hierarchy and Test Scene should look and act similar to the video below:
When you've finished, your level should look and act similar to the video below:
At this point, you have a very simple but playable level that is ready for some polish! In the next lesson, you will add a Level Select screen that will allow your player to select new levels to play.