41 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
}