I can't get player to respawn over network.
I am using a Unity Platforming Demo and can not get the player to respawn to the proper place over the network. The funny thing is, every once in a while, maybe 1/8, he'll respawn (which is just setting his position) works. For the most part, the player's position ends up being where he was last (which is falling offscreen)
I am using the a server-side kill trigger, and a ClientRPC to respawn. I am setting the position both in the server script and client script. I've tried many enumerations and combinations, even setting the transform of the rigid body or collision body transforms.
It seems that nothing gets this to work properly, and the player always ends up falling offscreen.
Here is my clientRPC for respawning.
[ClientRpc]
public void RpcRespawn (Vector3 pos)
{
Debug.Log("player respawned at position: " + pos.ToString());
gun.enabled = true;
playerControl.enabled = true;
health = 50f;
healthBar.enabled = true;
UpdateHealthBar();
gameObject.transform.position = pos;
respawned = true;
}
[ClientRpc]
public void RpcShutdown()
{
Debug.Log("player shutdown");
gun.enabled = false;
playerControl.enabled = false;
healthBar.enabled = false;
}
Also, here is my serve-rside code
IEnumerator ReloadGame()
{
// ... pause briefly
while (timeToRespawn.Count > 0)
{
yield return new WaitForFixedUpdate();
for(int i = 0; i < timeToRespawn.Count; i++)
{
timeToRespawn[i] -= Time.deltaTime;
if (timeToRespawn[i] < 0)
{
//MAKE RPC CALL HERE!
Debug.Log("Time to respawn count: " + timeToRespawn.Count.ToString() + " ip address: ");
GameObject player = objsToRespawn[i];
startPosition = NetworkManager.singleton.GetStartPosition().transform.position;
player.GetComponent<PlayerHealth>().RpcRespawn(startPosition);
player.transform.position = startPosition;
timeToRespawn.RemoveAt(i);
objsToRespawn.RemoveAt(i);
//Debug.Log("Player Respawned" + player.name + "at position: " + startPosition.ToString());
break;
}
}
}
}
Answer by Slences · Nov 10, 2015 at 08:16 PM
I'm not sure why you call
player.transform.position = startPosition;
after
player.GetComponent<PlayerHealth>().RpcRespawn(startPosition);
since you're setting the Gameobjects' position in the RpcRespawn method.
I'm not sure how to fix your problem, but maybe showing you my working respawn script will help you out:
// Called on server
foreach (GameObject player in getPlayerGameObjects())
{
PlayerStats pStats = player.GetComponent<PlayerStats>();
PlayerState pState = player.GetComponent<PlayerState>();
// Some stat resets that need to happen on server to the client
pStats.isDead = false;
pStats.health = pStats.maxHealth;
// Client side stuff that needs to be called for respawning
pState.RpcPlayerHandleSpawn();
// And finally set the players' position
SpawnPlayerToPosition(player, spawnPoints[i].transform.position);
}
[Server]
void SpawnPlayerToPosition(GameObject player, Vector3 pos)
{
player.GetComponent<PlayerState>().RpcSetPlayerPosition(pos);
}
[ClientRpc]
public void RpcSetPlayerPosition(Vector3 pos)
{
ctm.targetPosition = pos;
transform.position = pos;
}
[ClientRpc]
public void RpcPlayerHandleSpawn()
{
if (!isLocalPlayer)
return;
// Other client side stuff etc...
GetComponent<CapsuleCollider>().enabled = true;
GetComponent<CharacterController>().enabled = true;
}
Hope that helps!
Your answer
Follow this Question
Related Questions
Multiplayer Unet spawn on second player connect. 0 Answers
How can I read In a twitter # 1 Answer
Designing a multiplayer lobby for LAN 1 Answer
Unet - whats the best approach for a global game manager? 0 Answers
Bypassing network match password 0 Answers