Testing Player Movement Online

Checked with version: 5.3

-

Difficulty: Intermediate

Test Player Movement on the Host

At this point, the Player GameObject only moves on the Client.

It is not networked in any way.

To test:

  • Enter Play Mode.

In Play Mode, the NetworkManagerHUD will show the default user interface:

description

  • Click the LAN Host button on the in-game UI to start the game as the Host.

The NetworkManager will create a new player GameObject in the scene from the referenced player prefab. The NetworkManagerHUD will change the controls to show that the Server is active.

description

This game is running as a “Host” - which is a Server and a Client in the same process. For more information, please see the page on Network Concepts.

To test moving the player GameObject in the scene:

  • Press the WASD or arrow keys to move and turn the player

To stop hosting the game:

  • Select the “Stop” button on the in-game UI.

This will return the game to offline-mode.

To end this test:

  • Exit Play Mode in the editor.

Test Player Movement on a Client

To test player movement on a Client there will need to be two instances of the game running simultaneously; one of which will be the Host. One instance can be run from the editor, but the second needs to be run from a build of the game, so first we must create a build of the current project if we are to test movement on a client.

  • Add the scene Main to the build settings window.
  • Save the project.
  • Build and Run this scene only as a standalone application.
  • When the build is running, select the windowed checkbox and a choose a resolution low enough to have on screen alongside the editor.

The standalone player will now start and show the in-game UI from the NetworkManagerHUD.

  • Click the Host button from the in-game UI to start this game as a Host.

A player GameObject should be created by the NetworkManager. To test moving the Host’s player GameObject in the scene:

  • Press the WASD or arrow keys to move and turn the player

After moving the player GameObject in the scene:

  • Return to Unity.
  • Enter Play Mode

The game will now run in the editor and show the in-game UI from the NetworkManagerHUD.

  • Click the LAN Client button from the in-game UI to connect to the Host as a Client.

There should be two player GameObjects; one for the local player on the Host and one for the remote player for this Client.

To test moving the Client’s player GameObject in the scene:

  • Press the WASD or arrow keys to move and turn the Client's player

Note how both player GameObjects move in the scene using the same input.

  • Switch back to the standalone player.

Note how the player GameObjects are not in the same location on the Host as they are on the Client.

This is because the PlayerController script is not network-aware. In the current situation, both player GameObjects have the same script attached. Both are processing the same input in each separate instance of the game. The Host and Client are aware of each other, and the NetworkManager has created a player GameObject in each instance of the game for each player, but neither player GameObject is communicating with the Host so their positions are not tracked by the NetworkManager and are therefore not being synchronized.

  • Close the standalone player.
  • Return to Unity.
  • Exit Play Mode.

Related documentation