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

41 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class DeathZone : MonoBehaviour
{
public Animator animator;
public GameObject fadeScreen;
private GameObject player;
[SerializeField] private RespawnManager respawnManager;
[SerializeField] private PlayerInput _playerInput;
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.
IEnumerator RespawnPlayer()
{
animator.SetTrigger("IsDead");
_playerInput.enabled = false;
yield return new WaitForSeconds(0.5f);
fadeScreen.GetComponent<Animator>().SetTrigger("fadeToBlack");
yield return new WaitForSeconds(1.5f);
player.transform.position = respawnManager.GetRespawnPoint();
fadeScreen.GetComponent<Animator>().SetTrigger("fadeToClear");
yield return new WaitForSeconds(0.5f);
_playerInput.enabled = true;
yield return null;
}
}