- Home /
Player falls through plane/floor (Multiplayer)
Hello
I created two projects: Client and Server
On my server and client I have a "Player" prefab. This prefab has a network view on it as well as a rigidbody.
My server runs the following code:
When I click on a button:
Network.InitializeServer(10, 25000, false);
When a player disconnects and I want to remove him:
void OnPlayerDisconnected(NetworkPlayer player)
{
Network.RemoveRPCs(player);
Network.DestroyPlayerObjects(player);
}
When a player connects:
void OnPlayerConnected(NetworkPlayer player)
{
Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
}
My question is do I have to create the same level for the player on my server as well as on my client?
My client has a simple "Plane" which he falls on when he connects, but if I dont create the same "Plane" on my server my "Player" falls through the plane on the client side.
It seems highly inconvenient to do it this way. Also is this the correct way? Im unsure about where to instantiate gameobjects (Server or client)?
Answer by syclamoth · Jan 25, 2012 at 11:17 PM
If you are using 'Network.Instantiate' you should always use it on the client that should be in 'control' of that object. If you instantiate it from the server-side, the server ends up having 'control' over every object! If you want the server to have control over when the players get instantiated, you can manage it with a player RPC. Given that every player has an object assigned to them that can receive RPCs, you can use
networkView.RPC("SpawnPlayer", networkView.owner);
and then have a procedure called 'SpawnPlayer' that does the network Instantiation.
Another thing- I don't see what's so inconvenient about the server having to create the same level as all the clients. It makes sense for the server to know as much as possible about the environment, even if the server isn't also another player! And if the server is another player, then you don't need to worry about the environment thing at all, because naturally they will be in the same level as everyone else.
Thanks for the answer.
I see what you mean about it, but in my head its double the work, having a level where you edit one thing on the client side then having to edit the same thing on the server side as well.
But I guess I will just design the level/scene first and export it to my client after :)
By the way how do I remove a "Player (Clone)" gameobject from the network?
I know about "Network.Destroy" and I would guess it has to be done when at OnPlayerDisconnected, but I cant seem to wrap my head around it (Nor get it to work :-P)
I don't follow- it's no more work, because the client and the server use the same scene, in the same project!
If you want to remove a player's owned objects, you can use
Network.DestroyPlayerObjects(networkPlayer);
from the server to destroy all objects whose owners are that player. Use it from OnPlayerDisconnected. The reason you can't get it working, is probably the same problem as with the instantiation- you need the players to 'own' their own characters.
Again thank you, I appreciate you taking your time to answer :)
I can follow you in the sense if its the same project both running the client and the server.
But in my case I have a stand alone server (Project) accepting clients.
I might just have to integrate the server into the client
$$anonymous$$aybe i'm just misunderstanding this.
Yeah, that's what I'm saying. I see no reason not to have the server and client in the same project. In fact, I see separating them as creating work for yourself for no good reason. The whole 'client-server separation thing' makes sense on a low level, but Unity as a whole operates on a much higher level than that. You should be concentrating on desired behaviour, not how that behaviour occurs.