mirror of
https://github.com/Leahnaya/TheKingsRace.git
synced 2026-08-26 12:24:15 -05:00
Fixed Pitfall Respawning On Clients
This commit is contained in:
@@ -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<NetworkObject>().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<PlayerInventory>().UpdateEquips(playerData.pInv.NetworkItemList, this.gameObject.GetComponent<InventoryManager>().ItemDict);
|
||||
_runner.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientID, null, true);
|
||||
|
||||
// Turn on camera if the player is the host
|
||||
if (NetworkManager.Singleton.LocalClientId == clientID) {
|
||||
GameHandler.FindGameObjectInChildWithTag(_runner, "PlayerCam").GetComponent<Camera>().enabled = true;
|
||||
GameHandler.FindGameObjectInChildWithTag(_runner, "PlayerCam").GetComponent<AudioListener>().enabled = true;
|
||||
GameHandler.FindGameObjectInChildWithTag(_runner, "PlayerCam").GetComponent<PlayerCam>().enabled = true;
|
||||
|
||||
_runner.GetComponentInChildren<PlayerMovement>().enabled = true;
|
||||
} else {
|
||||
ClientRpcParams clientRpcParams = new ClientRpcParams
|
||||
try
|
||||
{
|
||||
character.GetComponent<NetworkObject>().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<PlayerInventory>().UpdateEquips(playerData.pInv.NetworkItemList, this.gameObject.GetComponent<InventoryManager>().ItemDict);
|
||||
_runner.GetComponent<NetworkObject>().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<Camera>().enabled = true;
|
||||
GameHandler.FindGameObjectInChildWithTag(_runner, "PlayerCam").GetComponent<AudioListener>().enabled = true;
|
||||
GameHandler.FindGameObjectInChildWithTag(_runner, "PlayerCam").GetComponent<PlayerCam>().enabled = true;
|
||||
|
||||
_runner.GetComponentInChildren<PlayerMovement>().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<NetworkObject>().OwnerClientId == clientId)
|
||||
{
|
||||
Debug.LogError("Found");
|
||||
GameHandler.FindGameObjectInChildWithTag(character, "PlayerCam").GetComponent<Camera>().enabled = true;
|
||||
GameHandler.FindGameObjectInChildWithTag(character, "PlayerCam").GetComponent<AudioListener>().enabled = true;
|
||||
GameHandler.FindGameObjectInChildWithTag(character, "PlayerCam").GetComponent<PlayerCam>().enabled = true;
|
||||
|
||||
character.GetComponentInChildren<PlayerMovement>().enabled = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Failed to find");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user