lucid-super-dream/Assets/Scripts/DisableOnDeath.cs

43 lines
1.0 KiB
C#
Raw Normal View History

2021-01-05 13:10:20 +01:00
using DG.Tweening;
using UnityEngine;
using Weapons.Scripts;
[RequireComponent(typeof(EntityHealth))]
public class DisableOnDeath : MonoBehaviour
{
private EntityHealth _health;
2021-01-06 16:10:11 +01:00
private Renderer _renderer;
private Color _color;
2021-01-05 13:10:20 +01:00
private void Awake()
{
_health = GetComponent<EntityHealth>();
2021-01-06 16:10:11 +01:00
_renderer = GetComponentInChildren<Renderer>();
_color = _renderer.material.color;
2021-01-05 13:10:20 +01:00
}
private void OnEnable()
{
_health.Die += Die;
2021-01-06 16:10:11 +01:00
_renderer.material.color = _color;
2021-01-05 13:10:20 +01:00
}
private void OnDisable()
{
_health.Die -= Die;
}
private void Die()
{
var oldName = gameObject.name;
gameObject.name = "disabled";
2021-01-06 16:10:11 +01:00
_renderer.material.DOColor(Color.white, 0.1f);
_renderer.material.DOFade(0, 0.2f).SetDelay(0.3f);
transform.DOScale(Vector3.one * 1.5f, 0.5f).SetEase(Ease.OutQuint).OnComplete(() =>
2021-01-05 13:10:20 +01:00
{
gameObject.name = oldName;
gameObject.SetActive(false);
_health.Reset();
});
}
}