Gameplay.SteamDamage - robblofield/TomeboundDocs GitHub Wiki

Gameplay.SteamDamage

Author: Shahrzad Beevis
Last Updated: 02/05/2024

Overview

A script that controls the damage effect that a steam column has on an object.

Dependencies

  • Slab.prefab
  • Health.cs

Contents

  • Use case
  • Breakdown of code
    • Using Statements
    • Class Declaration
    • Method/OnTriggerEnter(Collider collision)
  • Future Expansion
  • Full Code Reference

Use case

This script can be used to damage players, enemies and objects with steam columns.

Breakdown of Code

Using Statements

using UnityEngine;

Class Declaration

public class SteamDamage : MonoBehaviour

OnTriggerEnter(Collider collision)

   private void OnTriggerEnter(Collider collision)
{
    if (collision.gameObject.tag == "Player")
    {
        var healthComponent = collision.GetComponent<Health>();
        if (healthComponent != null)
        {
            healthComponent.TakeDamage(1);
            Debug.Log("Player took damage!");
        }
        else
        {
            Debug.LogWarning("Player object doesn't have a Health component!");
        }
    }
    
    if (collision.gameObject.tag == "Barrel")
    {
        Destroy(collision.gameObject);
    }

}

This method detects collisions with GameObjects tagged as "Player" or "Barrel" in the game scene. If the collision involves a "Player", it attempts to reduce the player's health by accessing its Health component and calling the TakeDamage method within the Health.cs script with a damage value of 1. It then also logs a message to the console indicating that the player took damage.

If the collision involves a "Barrel", the code destroys the barrel GameObject. Additionally, if the GameObject triggering the collider lacks a Health component when tagged as "Player", it logs a warning message to the console.

Future Expansion

N/A

Full Code Reference

using UnityEngine;

public class SteamDamage : MonoBehaviour
{
    private void OnTriggerEnter(Collider collision)
    {
        if (collision.gameObject.tag == "Player")
        {
            var healthComponent = collision.GetComponent<Health>();
            if (healthComponent != null)
            {
                healthComponent.TakeDamage(1);
                Debug.Log("Player took damage!");
            }
            else
            {
                Debug.LogWarning("Player object doesn't have a Health component!");
            }
        }
        
        if (collision.gameObject.tag == "Barrel")
        {
            Destroy(collision.gameObject);
        }


    }

}
⚠️ **GitHub.com Fallback** ⚠️