2021-02-15 10:44:04 +01:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
public class SafeZone : MonoBehaviour
|
|
|
|
{
|
|
|
|
private GameObject player;
|
|
|
|
private bool isSafe = false;
|
|
|
|
|
2021-02-15 14:14:42 +01:00
|
|
|
[SerializeField] private RespawnManager _respawnManager;
|
2021-02-15 10:44:04 +01:00
|
|
|
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
|
|
{
|
2021-02-15 17:23:29 +01:00
|
|
|
// Check to see if other collider is Player. If true set player to other game object.
|
2021-02-26 15:50:30 +01:00
|
|
|
if (other.gameObject.CompareTag("Player"))
|
2021-02-15 14:14:42 +01:00
|
|
|
{
|
|
|
|
player = other.gameObject;
|
2021-02-26 18:08:41 +01:00
|
|
|
isSafe = true;
|
2021-02-15 14:14:42 +01:00
|
|
|
}
|
2021-02-15 10:44:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
|
|
{
|
2021-02-15 17:23:29 +01:00
|
|
|
// Check if other game object is Player.
|
2021-02-26 18:08:41 +01:00
|
|
|
if (other.gameObject.CompareTag ("Player"))
|
2021-02-15 14:14:42 +01:00
|
|
|
{
|
|
|
|
isSafe = false;
|
|
|
|
}
|
2021-02-15 10:44:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void FixedUpdate()
|
|
|
|
{
|
2021-02-15 17:23:29 +01:00
|
|
|
// Sets the respawn point to the current player position as long as they are within a safe zone.
|
2021-02-15 14:14:42 +01:00
|
|
|
if (isSafe)
|
2021-02-15 10:44:04 +01:00
|
|
|
{
|
2021-02-15 14:14:42 +01:00
|
|
|
SetRespawnPoint(player.transform.position);
|
2021-02-15 10:44:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 14:14:42 +01:00
|
|
|
private void SetRespawnPoint(Vector3 _respawnPoint)
|
2021-02-15 10:44:04 +01:00
|
|
|
{
|
2021-02-15 17:23:29 +01:00
|
|
|
// Sets respawn point inside the respawn manager.
|
|
|
|
_respawnManager.SetRespawnPoint(_respawnPoint);
|
2021-02-15 10:44:04 +01:00
|
|
|
}
|
|
|
|
}
|