TheKingsRace/Assets/Scripts/Environment/ResetZonesGlobal.cs
Julia Butenhoff 3f51b34aec Added a Button to Reset to Last Checkpoint
- Unable to test to verify if it works until the Respawn Positions are
  in the game scene
2022-02-28 14:03:46 -06:00

46 lines
1.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MLAPI;
using MLAPI.Messaging;
public class ResetZonesGlobal : MonoBehaviour {
private bool cooldown = false;
[SerializeField]
private int RespawnZone;
private void OnTriggerEnter(Collider other) {
if (cooldown == false && other.transform.gameObject.tag == "PlayerTrigger" && other.gameObject.transform.root.gameObject.GetComponent<NetworkObject>().OwnerClientId == NetworkManager.Singleton.LocalClientId) {
SetClientRespawnZoneServerRPC(other.gameObject.transform.root.gameObject.GetComponent<NetworkObject>().OwnerClientId, RespawnZone);
cooldown = true;
StartCoroutine(CoolItDown());
}
}
[ServerRpc(RequireOwnership = false)]
private void SetClientRespawnZoneServerRPC(ulong clientID, int RespawnZone) {
SetClientRespawnZoneClientRPC(clientID, RespawnZone);
}
[ClientRpc]
private void SetClientRespawnZoneClientRPC(ulong clientID, int RespawnZone) {
// Get all players in the scene
GameObject[] playableCharacters = GameObject.FindGameObjectsWithTag("Player");
// Find our player first
foreach (GameObject character in playableCharacters) {
if (character.GetComponent<NetworkObject>().OwnerClientId == clientID) {
// Set their zone
character.GetComponent<ResetZones>().SetCurrentZone(RespawnZone);
}
}
}
IEnumerator CoolItDown() {
yield return new WaitForSecondsRealtime(1f);
cooldown = false;
}
}