- Home /
Changing player object UNET
Hello, people
I'm making a little game where players can change what object they're cotrolling. Like they start with a ball and they can roll it using WASD, and when they collide with some other ball tagged "PlayerBody", they will have control on that ball and lose it on old one.
Here's what I do (PlayerBodyScript.cs inheriting from NetworkBehaviour):
private void OnCollisionEnter(Collision col) {
if (tag == "Player") {
// ... some code about jump landing
if (col.gameObject.tag == "PlayerBody" && plyLastUpdated + 3f < Time.time){
// set new body if collided with PlayerBody
col.gameObject.GetComponent<PlayerBodyScript>().color = color;
if (isLocalPlayer){
// local player updates the camera
col.gameObject.GetComponent<PlayerBodyScript>().cam = GameObject.Find("Main Camera").transform;
GameObject.Find("Main Camera").GetComponent<CameraScript>().UpdateTarget(col.gameObject);
}
if (isServer) NetworkServer.ReplacePlayerForConnection(connectionToClient, col.gameObject, playerControllerId);
// unsetting player from this object
col.gameObject.GetComponent<PlayerBodyScript>().CmdChangeTagTo("Player");
CmdChangeTagTo("PlayerBody");
color = Color.white;
plyLastUpdated = Time.time;
}
}
}
But every time I touch other PlayerBody I got this error and user takes control over both bodies (I do not want to destroy old one, but just "move" user to the other "body"):
I am wondering what I'm doing wrong? I'm kinda new in UNET and Unity overall.
Thank you for any help.
Answer by Mentality · Jun 26, 2015 at 11:08 AM
As i can see: ReplacePlayerForConnection don't actually "replace" player controled object but adds new object to control.
So you need to use ReplacePlayerForConnection to your old object too.
PS: Could be wrong, i'll check it myself.
This replaces the player object for a connection with a different player object. The old player object is not destroyed. If a connection already has a player object, this can be used to replace that object with a different player object. This does NOT change the ready state of the connection, so it can safely be used while changin scenes.
This is how it's explained in ScriptingAPI
I think it should replace existing object for given playerControllerId completely as there's no method like NetworkServer.RemovePlayerForConnection( ... )
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
ReplacePlayerForConnection works but... 0 Answers
I have problem with synchronize multiplayer player position... 0 Answers
RPC activate object 0 Answers
Multiplayer| Attaching camera to player (if i am the owner 1 Answer