Menu
CoddyTech

Health and Hits

Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 59 of 63.

challenge icon

Challenge

Medium

The hunters hurt now. Keep everything from the earlier lessons. The Player has the Health script (the Inspector sets hp to 3), and the Hunter prefab sets a new knockback field to 12.

Fill in Health:

  • A public method TakeDamage(int amount) that takes amount off hp and logs what is left, as HP: 2.
  • When hp reaches 0, log Game over and turn off the player's Player script, so the arrows stop working.
  • Once hp is 0, further hits do nothing: no change and no log.

Add to Hunter:

  • A knockback field.
  • When a hunter bumps into something that has a Health (only the player has one), call its TakeDamage(1) and knock it back with an Impulse of knockback, straight away from the hunter across the ground.

Press Play and see how long you last, then Check.

Try it yourself

using UnityEngine;

public class Health : MonoBehaviour
{
    // A later lesson fills this in: the player's hit points.
}

All lessons in Physics and Gameplay