revival/game/Assets/Scripts/Death & Respawn/PlayerDeath.cs

41 lines
1.1 KiB
C#
Raw Normal View History

2021-03-12 12:52:56 +01:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Ktyl.Util;
public class PlayerDeath : MonoBehaviour
{
public Animator animator;
public GameObject fadeScreen;
private GameObject player;
[SerializeField] private SerialVector3 _respawnPosition;
[SerializeField] private GameEvent _playerDeath;
[SerializeField] private GameEvent _playerRespawn;
// Corountine to trigger death animation, disable player movement, play fade,
// respawn player at last safe position and then play fade in animation and re-enable player movement.
public IEnumerator RespawnPlayer()
{
animator.SetTrigger("IsDead");
_playerDeath.Raise();
yield return new WaitForSeconds(0.5f);
fadeScreen.GetComponent<Animator>().SetTrigger("fadeToBlack");
yield return new WaitForSeconds(1.5f);
// move player to respawn position
player.transform.position = _respawnPosition;
fadeScreen.GetComponent<Animator>().SetTrigger("fadeToClear");
yield return new WaitForSeconds(0.5f);
_playerRespawn.Raise();
yield return null;
}
}