Files
Deroc_Virtual_3D/Assets/FPS/Scripts/Game/Shared/Damageable.cs
T
Alex38Lyon 878ea46cac update
2025-06-03 12:00:47 +02:00

48 lines
1.4 KiB
C#

using UnityEngine;
namespace Unity.FPS.Game
{
public class Damageable : MonoBehaviour
{
[Tooltip("Multiplier to apply to the received damage")]
public float DamageMultiplier = 1f;
[Range(0, 1)] [Tooltip("Multiplier to apply to self damage")]
public float SensibilityToSelfdamage = 0.5f;
public Health Health { get; private set; }
void Awake()
{
// find the health component either at the same level, or higher in the hierarchy
Health = GetComponent<Health>();
if (!Health)
{
Health = GetComponentInParent<Health>();
}
}
public void InflictDamage(float damage, bool isExplosionDamage, GameObject damageSource)
{
if (Health)
{
var totalDamage = damage;
// skip the crit multiplier if it's from an explosion
if (!isExplosionDamage)
{
totalDamage *= DamageMultiplier;
}
// potentially reduce damages if inflicted by self
if (Health.gameObject == damageSource)
{
totalDamage *= SensibilityToSelfdamage;
}
// apply the damages
Health.TakeDamage(totalDamage, damageSource);
}
}
}
}