This commit is contained in:
manhduyhoang90
2026-05-26 09:46:57 +07:00
commit 167a617e09
1758 changed files with 1757605 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
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);
}
}
}
}