41 lines
1.1 KiB
C#
41 lines
1.1 KiB
C#
using UnityEngine;
|
||
using System.Collections.Generic;
|
||
using UnityEngine.Events;
|
||
|
||
[RequireComponent(typeof(Collider), typeof(Rigidbody))]
|
||
public class Damager : MonoBehaviour
|
||
{
|
||
[SerializeField] private float Damage;
|
||
[SerializeField] private List<GameObject> ImpactEffects;
|
||
|
||
public UnityEvent BeforeDestroy;
|
||
|
||
private GameObject Owner;
|
||
public void Initialize(GameObject owner, float damage)
|
||
{
|
||
Owner = owner;
|
||
Damage = damage;
|
||
}
|
||
|
||
|
||
private void OnTriggerEnter(Collider other)
|
||
{
|
||
if (other.TryGetComponent<Hitbox>(out var hitbox))
|
||
{
|
||
//Коллизия с собой
|
||
if (hitbox.Owner == this.Owner) return;
|
||
|
||
hitbox.TakeDamage(Damage, transform.position);
|
||
|
||
if(ImpactEffects != null && ImpactEffects.Count > 0)
|
||
{
|
||
var effect = ImpactEffects[Random.Range(0, ImpactEffects.Count)];
|
||
Instantiate(effect, transform.position, Quaternion.identity);
|
||
}
|
||
BeforeDestroy?.Invoke();
|
||
}
|
||
|
||
Destroy(gameObject);
|
||
}
|
||
}
|