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

71 lines
1.9 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;
2021-01-09 17:08:57 +01:00
private Color _color1;
private Color _color2;
private const string COLOR_1 = "_Color1";
private const string COLOR_2 = "_Color2";
2021-01-06 16:10:11 +01:00
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>();
2021-01-09 17:08:57 +01:00
_color1 = _renderer.material.GetColor(COLOR_1);
_color2 = _renderer.material.GetColor(COLOR_2);
2021-01-05 13:10:20 +01:00
}
private void OnEnable()
{
_health.Die += Die;
2021-01-09 17:08:57 +01:00
_renderer.material.SetColor(COLOR_1, _color1);
_renderer.material.SetColor(COLOR_2, _color2);
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-09 17:08:57 +01:00
float x = 0;
DOTween.To(
() => x,
t =>
{
var c1 = Color.Lerp(_color1, Color.white, t);
var c2 = Color.Lerp(_color2, Color.white, t);
_renderer.material.SetColor(COLOR_1, c1);
_renderer.material.SetColor(COLOR_2, c2);
_renderer.material.SetFloat("_Alpha", 1.0f-t);
},
1.0f,
0.1f);
// _renderer.material.DOColor(Color.white, COLOR_1, 0.1f);
// _renderer.material.DOColor(Color.white, COLOR_2, 0.1f);
// _renderer.material.DOFade(0, 0.1f).SetDelay(0.2f);
2021-01-07 11:05:19 +01:00
transform.DOScale(Vector3.one * 1.5f, 0.3f).SetEase(Ease.OutQuint).OnComplete(() =>
2021-01-05 13:10:20 +01:00
{
gameObject.name = oldName;
gameObject.SetActive(false);
_health.Reset();
});
}
}