revival/game/Assets/Scripts/Checkpoint/SafeZone.cs

47 lines
1.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class SafeZone : MonoBehaviour
{
private GameObject player;
private bool isSafe = false;
[SerializeField] private RespawnManager _respawnManager;
private void OnTriggerEnter(Collider other)
{
// Check to see if other collider is Player. If true set player to other game object.
if (other.gameObject.name == "Player")
{
isSafe = true;
player = other.gameObject;
}
}
private void OnTriggerExit(Collider other)
{
// Check if other game object is Player.
if (other.gameObject.name == "Player")
{
isSafe = false;
}
}
private void FixedUpdate()
{
// Sets the respawn point to the current player position as long as they are within a safe zone.
if (isSafe)
{
SetRespawnPoint(player.transform.position);
}
}
private void SetRespawnPoint(Vector3 _respawnPoint)
{
// Sets respawn point inside the respawn manager.
_respawnManager.SetRespawnPoint(_respawnPoint);
}
}