In this lesson, you will create a Projectile that will move toward an Enemy. Upon reaching the Enemy, it will apply damage. If an enemy receives enough damage, it will be destroyed.
Before your turrets can shoot a projectile, you must first create a Projectile Prefab to spawn.
weapon-ammo-arrow
but you can use any model you like)Projectile
MonoBehaviour ScriptProjectile
component to the PrefabWhen you're finished it should look similar to the image below:
It is often quicker to create a new scene to test your Prefabs and Scripts to see if they work in a constrained environment before putting them into your game.
Within the Projectile Test Scene, you will set up a situation to determine if the Projectile is working as intended.
Before you are able to create your test, you will need to add health and damage to your Enemy.
Health
MonoBehaviour Scriptfloat BaseHealth
propertyfloat Damage
propertyProjectile
script float Speed
property, set the default value to 2float Damage
property, set the default value to 1Transform Target
propertyBaseHealth
to 2When you're finished, your test scene should look similar to the video below:
Update
method to your Projectile
ScriptProjectile
should rotate to face the TargetProjectile
should move toward the Target at Speed
units per secondProjectile
is sufficiently close to the Target display a "Hit" message in the consoleNullReferenceExceptions
that you encounterWhen you have finished the challenge, it should look similar to the video below:
When the Projectile has reached its target, it should remove itself from the scene. This can be done using the Object.Destroy(Object)
method.
Object.Destroy
when the Projectile
has reach its TargetNote: You should destroy gameObject
which is the Game Object the Projectile
component is attached to.
Next, you will add an ApplyHit(Projectile)
method to your Health
script that will add Projectile.Damage
.
Health
scriptpublic void ApplyHit(Projectile)
Health.Damage
by Projectile.Damage
Before the Projectile
is destroyed, you must call Health.ApplyHit
specifying that this
Projectile
has reached the Target. However, because Target
is a Transform
, you cannot call the ApplyHit
method directly. Furthermore, the Target
is actually a child of the Enemy Prefab. To find the Health
component attached to the Parent object, you can use the Component.GetComponentInParent
method to find the Health
component.
Projectile
ScriptTarget.GetComponentInParent<Health>()
to retrieve the Health
component of the Target
Health
component was found, call ApplyHit(this)
Damage
property updates properly in your Test Projectile Scene Finally, update your Health script to destroy the enemy and remove it from the Scene if it has taken damage greater than or equal to its BaseHealth
.
When you have finished, your Test Projectile Scene should look similar to the video below:
The test you have created is for the best case scenario. That is, the Enemy is destroyed after receiving 2 damage. What happens if there is a 3rd projectile?
Depending on how you implemented your Projectile, it may throw a NullReferenceException
when the Target is destroyed. It may also simply stop moving when the Target is destroyed. If you check in the Inspector, you will see that the Target is marked as Missing. This is because it was destroyed.
As a developer, you will often need to account for situations that are not ideal but are technically possible during game play. These are often referred to as Edge Cases because they exist at the "boundaries" of what is expected. As you continue to develop your skills, you will begin to develop an intuition for Edge Cases and be able to anticipate many (but not all) of them.
There are many possible ways to handle this particular Edge Case. One of the simplest solutions is to destroy the Projectile if it has no Target.
Projectile
script such that if the Target
is null
, the Projectile is removed from the SceneWhen you have successfully implemented this challenge, your scene should act similar to the video below:
With your Projectile Prefab working, it is time to add a script that allows your turrets to fire them.