Health and Hits
Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 59 of 63.
Challenge
MediumThe 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 takesamountoffhpand logs what is left, asHP: 2. - When
hpreaches 0, logGame overand turn off the player'sPlayerscript, so the arrows stop working. - Once
hpis 0, further hits do nothing: no change and no log.
Add to Hunter:
- A
knockbackfield. - 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 ofknockback, 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
1Rigidbody
Gravity and MassPush with AddForceJump with an ImpulseFixedUpdate for PhysicsVelocityKinematic BodiesRecap: Launch Pad2Collisions
OnCollisionEnterWhat Did I Hit?Break on ImpactStanding on the GroundBounce and FrictionCollider ShapesRecap: Breakable Wall