Restoring Health

版本检查: 2018.1

-

难度: 新手

In this tutorial we will allow Ellen to restore health lost after an attack from an enemy. This will use Ellen’s Damageable component and use ResetDamage() to fully heal her.

Add a Spitter Enemy

To ensure Ellen takes damage and to allow you to quickly play test the Health systems, we will place a Spitter enemy in the scene near Ellen's starting point.

Go to Hierarchy view in your open scene and locate the Spiller in the folders 3DGamekit > Prefabs > Characters > Enemies > Spitter

Drag the Spitter into the Scene view, and adjust its position and rotation to suit your needs. Ensure that the blue detection circle has Ellen within it so the Spitter will see Ellen as soon as you enter Play mode. This will save you time in running to an enemy that may be located farther away.

To adjust the Detection Radius, with the Spitter selected locate the Spitter Behaviour Script component in the Inspector view.

Change the Detection Radius field. 8 is used in this example but adjust the size to suit your particular setup if required.

Add a Health Crate

In the Hierarchy view, locate the HealthCrate prefab by either searching for "Health Crate" in the search box

or find it in the folders 3DGamekit > Prefabs > Interactables > HealthCrate

Drag the HealthCrate Prefab from the Hierarchy view into the Scene view and place the Prefab in a suitable location. Rotate and translate to your liking.

With the HealthCrate in the Scene view selected, in the Inspector view, notice the Interact On Trigger (Script) Component.

This script component allows the HealthCrate to call events on a list of GameObjects to allow interaction between them and the HealthCrate. In this case, these will occur when the player moves Ellen towards its Collider area.

Currently there is no GameObject or function set to restore Ellen’s health so if we try to collide with the HealthCrate in Play mode now, it won’t do anything!

Try it out, enter Play mode, make Ellen take damage from the Spitter and try to restore her health by colliding with the Health Crate.

Interact On trigger

To ensure that Ellen is healed fully, the GameObject in the Scene needs to be added to Interact On Trigger.

Note: If you prefer you can use the drag and drop approach for the following steps, however we will look at using the circle select approach instead.

If you decide to do this, please ensure you are using Ellen in the current scene from the Hierarchy view, not the prefab from the Project view.

Select the HealthCrate if it is deselected during Play mode.

In the blank Object field in On Enter(), press the Circle Select button

Select the Scene Tab from the top bar. Assets is selected by Default.

Search for Ellen (If you have renamed your player character, you should search for that name instead)

Select "Ellen" (the rest are related to specific parts).

Now that Ellen has been selected as the GameObject to interact with OnEnter(), it is time to select the function to restore health.

The Interact On Trigger (Script) the drop-down field next to Ellen currently says No Function,

In the drop-down menu, select Damageable > ResetDamage()

This step has now linked the HealthCrate and Ellen within the scene to allow health to be restored as it will call ResetDamage() when Ellen enters the Collider.

Play your level now to test damage from the Spitter and colliding with the Health Crate will now fully restore her hearts.