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

50 lines
1.6 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using Ktyl.Util;
using UnityEngine;
using UnityEngine.InputSystem;
public class DeathZone : MonoBehaviour
{
public Animator animator;
public GameObject fadeScreen;
private GameObject player;
[SerializeField] private SerialVector3 _respawnPosition;
[SerializeField] private GameEvent _playerDeath;
[SerializeField] private GameEvent _playerRespawn;
private void OnTriggerEnter(Collider other)
{
// Checks to make sure other collider is the Player. Sets player variable as Player game object and starts the coroutine.
if (other.gameObject.name == "Player")
{
player = other.gameObject;
StartCoroutine(RespawnPlayer());
}
}
// 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;
}
}