Restoring Health

Tutorial

·

Beginner

·

+10XP

·

40 mins

·

(190)

Unity Technologies

Restoring Health

In this section we’ll cover how to create a Health Pack, which the player can use to restore health. This uses Ellen’s Damageable component with the command ResetDamage() to fully heal her.

Languages available:

1. Set the Scene Up

Let’s start by setting up the Scene so that Ellen takes damage when you enter Play mode. To do this, let’s place a Spitter enemy in the Scene near Ellen's starting point.

To place a Spitter, navigate to the Project view and go to 3DGamekitLite > Prefabs > Characters > Enemies > Spitter.

Drag the Spitter into the Scene view, adjust position and rotation to suit your needs. Ensure that the blue detection circle has Ellen within it so the Spitter can see her as soon as you enter Play mode. This should save you time in running to an enemy located farther away when you want Ellen to take damage.

Add a HealthCrate

In the Project view, locate the HealthCrate Prefab. Go to 3DGamekitLite > Prefabs > Interactables > HealthCrate. Alternatively, you can search for “HealthCrate” in the search box.

Drag the HealthCrate Prefab from the Hierarchy window into the Scene view. Rotate and position it however you like, as long as Ellen can get to it.

Select the HealthCrate and view it in the Inspector window. It has an Interact On Trigger component.

You can use this component to list GameObjects that you want the HealthCrate to interact with. Anything listed under On Enter () happens when the GameObject touches the HealthCrate. Anything listed under On Exit () happens when the GameObject stops touching the HealthCrate.

When you first place the HealthCrate Prefab in the Scene, it has a few events set in the On Enter () list, but there is nothing to restore Ellen’s health. If you press Play, take damage and then run to touch the HealthCrate, nothing happens.

Add Ellen to Interact on Trigger

To ensure that Ellen is healed fully, you need to add 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, ensure you are using Ellen in the current Scene from the Hierarchy window, not the Prefab from the Project view.

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

Select the Scene tab in the top bar.

Search for Ellen (or, if you have renamed your player character, search for that name instead).

Select Ellen. The other Prefabs are related to specific parts.

Now that you have put Ellen on the On Enter () list, the HealthCrate is programmed to interact with the Ellen GameObject when Ellen touches it. However, we still haven’t told it what that interaction should be. We need to set it to restore her health.

In the Interact On Trigger the drop-down field next to Ellen currently says No Function.

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

Now you’ve linked the HealthCrate and Ellen within the Scene, so that when Ellen touches the HealthCrate, Unity calls an event called ResetDamage() to set her damage back to its original value.

To test the HealthCrate, press Play, take damage from the Spitter, and then touch the HealthCrate. This time, touching the crate should fully restore Ellen’s health.

Complete this tutorial