35 lines
853 B
C#
35 lines
853 B
C#
using System;
|
|
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(BoxCollider2D))]
|
|
public class HitBox : MonoBehaviour
|
|
{
|
|
public bool DoesDealDamage;
|
|
public float DamageAmount;
|
|
public float hitStunDuration;
|
|
public Action<float, float> TakeDamage;
|
|
|
|
private BoxCollider2D _collider;
|
|
|
|
private void Start()
|
|
{
|
|
_collider = GetComponent<BoxCollider2D>();
|
|
}
|
|
|
|
private void OnTriggerStay2D(Collider2D other)
|
|
{
|
|
var otherHitBox = other.GetComponent<HitBox>();
|
|
if (otherHitBox != null && DoesDealDamage)
|
|
{
|
|
// do damage.
|
|
Physics2D.IgnoreCollision(_collider, other);
|
|
otherHitBox.TakeDamage(DamageAmount, hitStunDuration);
|
|
}
|
|
}
|
|
|
|
private void OnTriggerExit2D(Collider2D other)
|
|
{
|
|
Physics2D.IgnoreCollision(_collider, other, false);
|
|
}
|
|
}
|