Create Health and Ragdoll component and and them to enemy
This commit is contained in:
54
Assets/Scripts/Components/HealthComponent.cs
Normal file
54
Assets/Scripts/Components/HealthComponent.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using Sirenix.OdinInspector;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class HealthComponent : MonoBehaviour
|
||||
{
|
||||
[ReadOnly]
|
||||
[SerializeField]
|
||||
private int health;
|
||||
|
||||
[ReadOnly]
|
||||
[SerializeField]
|
||||
private bool isDead;
|
||||
|
||||
public UnityEvent onDied;
|
||||
|
||||
[Button]
|
||||
public void Setup()
|
||||
{
|
||||
health = 100;
|
||||
isDead = false;
|
||||
}
|
||||
|
||||
public void TakeDamage(int damage)
|
||||
{
|
||||
health -= damage;
|
||||
|
||||
if (health < 0)
|
||||
{
|
||||
health = 0;
|
||||
}
|
||||
|
||||
if (health == 0)
|
||||
{
|
||||
isDead = true;
|
||||
onDied.Invoke();
|
||||
}
|
||||
}
|
||||
|
||||
[Button]
|
||||
private void Take10Damage()
|
||||
{
|
||||
TakeDamage(10);
|
||||
}
|
||||
|
||||
[Button]
|
||||
private void Kill()
|
||||
{
|
||||
TakeDamage(health);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user