- Home /
Players not moving properly - Network Multiplayer
hey guys, im having a problem with my game. I have a server and client which work fine but when a 2nd player spawns they can be controlled from the 1st player. and they keep falling through floor and glitching out... I have a player prefab which DOES have a network view.. dunno whats going on. heres how im spawning my player
In the client:
void OnConnectedToServer() {
Network.Instantiate(PlayerPrefab, SpawnObj.transform.position, SpawnObj.transform.rotation, 0);
networkView.RPC("UserLoggedIn", RPCMode.AllBuffered, myUserName);
}
in the server:
[RPC]
void UserLoggedIn(string myUserName) {
PlayerLog.PlayerListAddEvent ("Player: " + myUserName);
eventLog.AddEvent ("Player: " + myUserName + ", Connected from " + "'" + "IPHERE" + ":" + "PORT HERE" + "'!");
}
Please help me out
can you post the script which controls the player movement, may be i can help !!
the player movement script is just the basic FPS one that comes with unity. Also now we cant see eachother unless i look through the server (which is a different scene but shows the players too)
Answer by flamy · Nov 11, 2013 at 09:22 AM
Enclose the whole content of Update in the FPS Movement script under a if loop like this
void Update()
{
if(networkView.isMine)
{
......
......
......
}
}
this way only the peer that instantiated the object would be able to move them.
"Also now we cant see eachother unless i look through the server (which is a different scene but shows the players too)"
regarding this issue, you are instantiating the prefab on connecting to the server, instead of that you should be loading the target scene whn the player joins the server and in the start of some script in the target scene use this Network.Instantiate call.
I guess this is the flow you have rite now.
1.Player 1 (server) joins the lobby.
2.player 1 host a game and enter demo scene.
3.Player 1 character is already inside the scene.
4.Now player 2 joins the lobby.
5.player 2 finds player 1 game and joins it.
6.Player 2 creates the character object through Network.Instantiate.
7.Player 2 loads demo scene. (or probably this is not happening as well..)..
Well the problem is at step 6 and 7. As soon as player joins u r creating his object which is in lobby scene(or similar). So once you load the original scene, the player object will get destroyed and hence u wont see him in your system. but in server which is already in the demoscene will be able to see it. that is wat is happening. So the correct flow should be,
1.Player 1 (server) joins the lobby.
2.player 1 host a game and enter demo scene.
3.Player 1 character is already inside the scene.
4.Now player 2 joins the lobby.
5.player 2 finds player 1 game and joins it.
6.Player 2 loads demo scene.
7.Player 2 creates the character object through Network.Instantiate from awake of some script. To do this check if Networt.isClient and if it is true instantiate (this is because the server would already have the player object.)
Hope it is helpful.
hey thanks for the comment. ill try it now, but in the meantime. Player 1 isnt the server - the server is a program ive made on its own in Unity (think of a $$anonymous$$ecraft server) then the players join it. so the server doesnt spawn a player
oh that is fine...but still the problem is in client rite.
And my flow is correct except the player spawning the objects rite!. If so the solution should work.
ok so whats happening now is i can control player 2 and player 2 can control me :( weirddd
well it is wierd... i dont knw why it is happening but try this loop ins$$anonymous$$d
if(networkView.owner == Network.player)
{
....
....
...
}
ok ill try, thank you
the default FPS has 3 scripts i believe: FPSInputController, Character$$anonymous$$otor, $$anonymous$$ouseLook
do i put the loop in every update for each of those scripts?