Чтобы ошибки не сыпало при старте на gameplay сцене А то OnDestroy отрабатывают и спамят консоль
52 lines
1.4 KiB
C#
52 lines
1.4 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.Events;
|
|
using System;
|
|
|
|
[RequireComponent(typeof(Collider), typeof(Rigidbody))]
|
|
public class Damager : MonoBehaviour
|
|
{
|
|
[Header("Эффекты")]
|
|
[SerializeField] private List<GameObject> ImpactEffects;
|
|
|
|
public event Action OnHit;
|
|
public static event Action<GameObject, GameObject> OnCollideHitbox; //arg1 - this.owner, arg2 - collidedHitbox.owner
|
|
|
|
// Динамически установленные значения
|
|
private GameObject owner;
|
|
private float damage;
|
|
|
|
public GameObject Owner => owner;
|
|
|
|
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();
|
|
|
|
SpawnImpactEffects();
|
|
}
|
|
}
|
|
|
|
private void SpawnImpactEffects()
|
|
{
|
|
if (ImpactEffects == null || ImpactEffects.Count == 0) return;
|
|
|
|
foreach (var effect in ImpactEffects)
|
|
{
|
|
Instantiate(effect, transform.position, transform.rotation);
|
|
}
|
|
}
|
|
} |