- Home /
UNET player respawn
First of all I'd like to state that I'm new to Unity, but an experienced programmer.
I'm trying to respawn the player over network. The idea is that when the player presses the reset button, the client sends a Command to the server that she should be respawned on one of the predetermined spawn points (game objects with Network Start Position component). I was hoping the server could have done this by itself, since when a new player connects it spawns him automatically on one of those spawn points.
I tried using NetworkServer.ReplacePlayerForConnection
like this:
[Command] void CmdRespawn( ) { var newPlayer = Instantiate( this.gameObject ); NetworkServer.Destroy( this.gameObject ); NetworkServer.ReplacePlayerForConnection( this.connectionToClient, newPlayer, this.playerControllerId ); }
Unfortunately the new player is spawned at the same location as the old one, which is not what I'm looking for.
Is there any elegant solution to this or should I just manually pick one of the spawn points and move the player over there?
Answer by mibix · Aug 31, 2015 at 09:36 AM
This work for me:
[Command]
void CmdRespawnSvr(){
var spawn=NetworkManager.singleton.GetStartPosition();
var newPlayer = ( GameObject) Instantiate(NetworkManager.singleton.playerPrefab, spawn.position, spawn.rotation );
NetworkServer.Destroy( this.gameObject );
NetworkServer.ReplacePlayerForConnection( this.connectionToClient, newPlayer, this.playerControllerId );
}