Damage System

Checked with version: 2017.3

-

Difficulty: Beginner

The damage system in the kit is composed of two components: Damager and Damageable scripts.

Adding a Damager script to a GameObject makes it able to inflict damage to any GameObject that has a Damageable component, as long as their settings match.

Damager

The Damager Component has events for collision with Damageable components and non Damageable components. The health calculation is done automatically and these events are used for triggering gameplay changes or visual effects.

The Damager component does not require a collider. It displays a box gizmo in the Scene View which you can use to tweak its size and position.

  • Damage: The amount of damage the GameObject inflicts on a Damageable (e.g Ellen or Chomper) when hit.

  • Offset and Size: these are the two parameters that define the Damager collision box. This is the outer box displayed on the Spikes in the Scene View as the outer box, it will inflict damage when this is collided with.

  • Offset Based On Sprite Facing: This setting allows the Damager to change position based on which direction a sprite is facing. For example, this is used on Ellen allowing the Damager to flip left and right depending on which way she is facing.

  • Sprite Renderer: The Sprite Renderer that the above setting uses.

  • Can Hit Triggers: Toggles whether the Damager collides with triggers (if checked) or totally ignore triggers (if unchecked).

  • Force Respawn: If this is enabled, the GameObject containing a Damageable immediately respawns to the latest checkpoint ( the Acid prefab uses this when the player falls into the water).

  • Ignore Invincibility: If enabled, the Damager still registers a ‘hit’ to the Damageable but does not remove health. In the example game, the Acid uses this so that the player always respawns, even if they are invincible from a previous enemy hit.

  • Hittable Layers: The Layers on which the Damager inflicts damage. Layers that are not selected do not receive damage.

  • On Damageable Hit: Events here play when a GameObject with a Damageable component is hit.

  • On Non Damageable Hit: Events here play when a GameObject with no Damageable component is hit.

Damageable

The Damageable Component receives damage from collisions of GameObjects with the Damager Component attached to them. There are events for when damage is received, health is restored, health changes, or complete depletion of health (death) occurs.

Contrary to the Damager, the Damageable component relies on colliders attached to the same GameObject, so in addition to adding a Damageable Script to your GameObject, don't forget to add colliders to define hittable zones.

  • Starting Health: Health level. Damagers remove their health at each impact. GameObjects with a Damageable Component die when this reaches 0.

  • Invulnerable After Damage: This Damageable becomes invulnerable for a given time after impact.

  • Invulnerability Duration: Length of time (in seconds) that this Damageable is considered invulnerable after impact (only applicable if previous setting is set).

  • Disable On Death: This setting disables the GameObject on death.

  • Center Offset: This is used to define where the centre of the Damageable is (an offset of (0,0,0) means it is on the GameObject position). Used to compute distance to the Damager.

  • On Take Damage: Events triggered when the GameObject receives damage.

  • On Die: Events triggered when the GameObject's health is depleted.

  • On Gain Health: Events triggered when a GameObject gains new health.

  • Persistent Type: See Persistent Data

  • Data Tag: See Persistent Data

Checkpoints

Find checkpoints the Project window, in the folder Prefabs > SceneControl

Each time the Player enters a Checkpoint collider, this Checkpoint becomes active. When they fall into the water (or get hit by any Damager that has the Force Respawn setting) they reappear at that Checkpoint.

Note: The Player reappears at the GameObject position, so make sure it is above ground.

Related tutorials