- Home /
[SOLVED]UNET Changing character classes during multiplayer game?
How do I change player prefab with the network manager during a live game? I want the user to be able to choose what class they want to play as like a Rogue, Mage, or Warrior. Then once the player dies they can choose a new class to spawn as.
The only closest examples of this mechanic I can find is people choosing a class in the lobby manager scene, but then once in game they are stuck with the player prefab for the rest of the match. I need to allow the player to dynamically change their player prefab during the game between response
How can I change the initial player prefab in the network manager during run time and sync it to the server for the other clients to see.
These folks are close :: but not quite there. Has anyone worked through this or have an idea. I've been searching up other documents and forums but nothing is hitting the nail on the head. Appreciate any and all help.
Answer by TestifyNow · Mar 09, 2017 at 04:53 PM
send [Comman]d, which in turn does [ClientRPC], in this RPC destroy player prefab, create a new one, and give the same player local authority over it.
Thank you, I was noticing some issues and looking over the code when RPC was co$$anonymous$$g up. Thank you for the correction. I will see how it goes.
Seems to fix a bit. I threw in this, I have a command function calling this RPC function. It gets the player connection, original prefab info, and set the colliders off. After it instantiates a new object, assigns old prefab info to new object, set prefab switched to true(no worries about this), destroys old object, and finally replaces prefab connect. There is one issue...
[ClientRpc]
void RpcSpawnPlayer()
{
//GameObject instance = Instantiate (testObj, testObj.transform.position, this.gameObject.transform.rotation);
//NetworkServer.SpawnWithClientAuthority (instance, connectionToClient);
var conn = GetComponent<NetworkIdentity> ().connectionToClient;
SetupPlayer playerStates = GetComponent<SetupPlayer> ();
gameObject.GetComponent<SetupPlayer> ().SetActiveState (false);
var newPlayer = Instantiate<GameObject> (testObj,playerStates.originalPos,transform.rotation);
//send player states to next prefab player
newPlayer.GetComponent<SetupPlayer> ().playerName = playerStates.playerName;
newPlayer.GetComponent<SetupPlayer> ().score = playerStates.score;
newPlayer.GetComponent<SetupPlayer> ().$$anonymous$$m = playerStates.$$anonymous$$m;
newPlayer.GetComponent<SetupPlayer> ().originalPos = playerStates.originalPos;
//makes sure starting prefab states only reset once
newPlayer.GetComponent<SetupPlayer> ().prefabSwitched = true;
Destroy (GetComponent<NetworkIdentity> ().gameObject);
NetworkServer.ReplacePlayerForConnection (conn,newPlayer,0);
}
The issue was originally happening on both client and host, but now it is just the host thanks to your lead. The host once changed seems to spawn in another position for a split second before beginning at the starting point(start spawn position). Is there any reason this code would have any issues?
Answer by Oakioats · Mar 12, 2017 at 06:07 PM
SOLVED: So after some jumping around managed to find a simple snippet of code to swap player prefabs.
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Networking pattern for team-based play 1 Answer
Networking Camera not attaching to instantiated object 2 Answers
Animation over network using rpcs 0 Answers