Gameplay.SteamDamage - robblofield/TomeboundDocs GitHub Wiki
A script that controls the damage effect that a steam column has on an object.
- Slab.prefab
- Health.cs
- Use case
- Breakdown of code
- Using Statements
- Class Declaration
- Method/OnTriggerEnter(Collider collision)
- Future Expansion
- Full Code Reference
This script can be used to damage players, enemies and objects with steam columns.
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);
}
}
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.
N/A
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);
}
}
}