From ade428ef8a6765a11f99a45e2f9188ad02581d74 Mon Sep 17 00:00:00 2001 From: Julia Butenhoff Date: Tue, 18 Jan 2022 11:46:26 -0600 Subject: [PATCH] Fixed Pitfall Respawning On Clients --- Assets/Scripts/Environment/Pitfall.cs | 83 +++++++++++++++++---------- UserSettings/EditorUserSettings.asset | 2 +- 2 files changed, 55 insertions(+), 30 deletions(-) diff --git a/Assets/Scripts/Environment/Pitfall.cs b/Assets/Scripts/Environment/Pitfall.cs index ab1705a..5f2b7ac 100644 --- a/Assets/Scripts/Environment/Pitfall.cs +++ b/Assets/Scripts/Environment/Pitfall.cs @@ -1,4 +1,5 @@ using MLAPI; +using MLAPI.Exceptions; using MLAPI.Messaging; using System.Collections; using System.Collections.Generic; @@ -45,43 +46,62 @@ public class Pitfall : NetworkBehaviour // Player found // Despawn them - character.GetComponent().Despawn(true); - } - } - - // Spawn the player - if (ServerGameNetPortal.Instance.clientIdToGuid.TryGetValue(clientID, out string clientGuid)) - { - if (ServerGameNetPortal.Instance.clientData.TryGetValue(clientGuid, out PlayerData playerData)) - { - // Spawn as player - _runner = Instantiate(runnerPrefab, RespawnPoint, Quaternion.Euler(0, -90, 0)).gameObject; - //Recreate Inventory - _runner.GetComponentInChildren().UpdateEquips(playerData.pInv.NetworkItemList, this.gameObject.GetComponent().ItemDict); - _runner.GetComponent().SpawnAsPlayerObject(clientID, null, true); - - // Turn on camera if the player is the host - if (NetworkManager.Singleton.LocalClientId == clientID) { - GameHandler.FindGameObjectInChildWithTag(_runner, "PlayerCam").GetComponent().enabled = true; - GameHandler.FindGameObjectInChildWithTag(_runner, "PlayerCam").GetComponent().enabled = true; - GameHandler.FindGameObjectInChildWithTag(_runner, "PlayerCam").GetComponent().enabled = true; - - _runner.GetComponentInChildren().enabled = true; - } else { - ClientRpcParams clientRpcParams = new ClientRpcParams + try + { + character.GetComponent().Despawn(true); + } catch (SpawnStateException e) { + Debug.Log("Exception"); + return; + } + + // Spawn the player + if (ServerGameNetPortal.Instance.clientIdToGuid.TryGetValue(clientID, out string clientGuid)) + { + if (ServerGameNetPortal.Instance.clientData.TryGetValue(clientGuid, out PlayerData playerData)) { - Send = new ClientRpcSendParams + // Spawn as player + _runner = Instantiate(runnerPrefab, RespawnPoint, Quaternion.Euler(0, -90, 0)).gameObject; + //Recreate Inventory + // FOLLOWING LINE WILL NEED TO BE UPDATED SINCE THIS WILL ONLY WORK FOR THE HOST AND NOT THE CONNECTED CLIENTS + _runner.GetComponentInChildren().UpdateEquips(playerData.pInv.NetworkItemList, this.gameObject.GetComponent().ItemDict); + _runner.GetComponent().SpawnAsPlayerObject(clientID, null, true); + + // Turn on camera if the player is the host + if (NetworkManager.Singleton.LocalClientId == clientID) { - TargetClientIds = new ulong[] { clientID } + GameHandler.FindGameObjectInChildWithTag(_runner, "PlayerCam").GetComponent().enabled = true; + GameHandler.FindGameObjectInChildWithTag(_runner, "PlayerCam").GetComponent().enabled = true; + GameHandler.FindGameObjectInChildWithTag(_runner, "PlayerCam").GetComponent().enabled = true; + + _runner.GetComponentInChildren().enabled = true; } - }; - // Spawn via RPC on the server - SpawnPlayerClientRpc(clientID, clientRpcParams); + else + { + // Spawn via RPC on the server + StartCoroutine(SpawnClient(clientID)); + } + } } } } } + // The client respawning needs a slight delay to allow for the spawn to properly sync up + IEnumerator SpawnClient(ulong clientID) + { + yield return new WaitForSecondsRealtime(1f); + + ClientRpcParams clientRpcParams = new ClientRpcParams + { + Send = new ClientRpcSendParams + { + TargetClientIds = new ulong[] { clientID } + } + }; + + SpawnPlayerClientRpc(clientID, clientRpcParams); + } + // Spawn in each player [ClientRpc] public void SpawnPlayerClientRpc(ulong clientId, ClientRpcParams clientRpcParams = default) @@ -93,12 +113,17 @@ public class Pitfall : NetworkBehaviour { if (character.GetComponent().OwnerClientId == clientId) { + Debug.LogError("Found"); GameHandler.FindGameObjectInChildWithTag(character, "PlayerCam").GetComponent().enabled = true; GameHandler.FindGameObjectInChildWithTag(character, "PlayerCam").GetComponent().enabled = true; GameHandler.FindGameObjectInChildWithTag(character, "PlayerCam").GetComponent().enabled = true; character.GetComponentInChildren().enabled = true; } + else + { + Debug.LogError("Failed to find"); + } } } } diff --git a/UserSettings/EditorUserSettings.asset b/UserSettings/EditorUserSettings.asset index 07f941b..e97a416 100644 --- a/UserSettings/EditorUserSettings.asset +++ b/UserSettings/EditorUserSettings.asset @@ -33,7 +33,7 @@ EditorUserSettings: value: 22424703114646680e0b0227036c6f02131b172b282d347e38271427fb flags: 0 RecentlyUsedScenePath-9: - value: 22424703114646680e0b0227036c78111b125507233d28242c20137df7ee3d2cfb + value: 22424703114646680e0b0227036c6f02131b172b282d347e38271427fb flags: 0 UnityRemoteCompression: value: 337f73