2021-02-15 10:44:04 +01:00
using System.Collections ;
using System.Collections.Generic ;
using UnityEngine ;
2021-02-15 17:23:29 +01:00
using UnityEngine.InputSystem ;
2021-02-15 10:44:04 +01:00
public class DeathZone : MonoBehaviour
2021-02-15 14:14:42 +01:00
{
public Animator animator ;
2021-02-15 10:44:04 +01:00
public GameObject fadeScreen ;
2021-02-15 14:14:42 +01:00
private GameObject player ;
[SerializeField] private RespawnManager respawnManager ;
2021-02-15 17:23:29 +01:00
[SerializeField] private PlayerInput _playerInput ;
2021-02-15 10:44:04 +01:00
private void OnTriggerEnter ( Collider other )
{
2021-02-15 17:23:29 +01:00
// Checks to make sure other collider is the Player. Sets player variable as Player game object and starts the coroutine.
2021-02-25 00:41:33 +01:00
if ( other . gameObject . tag = = "Player" )
2021-02-15 10:44:04 +01:00
{
2021-02-15 14:14:42 +01:00
player = other . gameObject ;
2021-02-15 17:23:29 +01:00
StartCoroutine ( RespawnPlayer ( ) ) ;
2021-02-15 10:44:04 +01:00
}
}
2021-02-15 17:23:29 +01:00
// 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 ( )
2021-02-15 10:44:04 +01:00
{
animator . SetTrigger ( "IsDead" ) ;
2021-02-15 17:23:29 +01:00
_playerInput . enabled = false ;
yield return new WaitForSeconds ( 0.5f ) ;
2021-02-15 10:44:04 +01:00
2021-02-15 17:23:29 +01:00
fadeScreen . GetComponent < Animator > ( ) . SetTrigger ( "fadeToBlack" ) ;
yield return new WaitForSeconds ( 1.5f ) ;
2021-02-15 10:44:04 +01:00
2021-02-15 17:23:29 +01:00
player . transform . position = respawnManager . GetRespawnPoint ( ) ;
fadeScreen . GetComponent < Animator > ( ) . SetTrigger ( "fadeToClear" ) ;
yield return new WaitForSeconds ( 0.5f ) ;
2021-02-15 10:44:04 +01:00
2021-02-15 17:23:29 +01:00
_playerInput . enabled = true ;
2021-02-15 10:44:04 +01:00
yield return null ;
}
}