32 lines
876 B
C#

using System;
using UnityEngine;
[RequireComponent(typeof(Collider), typeof(Rigidbody))]
public class Damager : MonoBehaviour
{
public event Action OnHit;
public static event Action<GameObject, GameObject> OnCollideHitbox; //arg1 - this.owner, arg2 - collidedHitbox.owner
private GameObject owner;
private float damage;
public void Initialize(GameObject owner, float damage)
{
this.owner = owner;
this.damage = damage;
}
private void OnTriggerEnter(Collider other)
{
if (owner != null && other.gameObject == owner) return;
if (other.TryGetComponent<Hitbox>(out var hitbox))
{
if (hitbox.Owner == owner) return;
hitbox.TakeDamage(damage, transform.position);
OnCollideHitbox?.Invoke(this.owner, hitbox.Owner);
OnHit?.Invoke();
}
}
}