Moving Your Player Around
Get your player objects moving around and synchronized!
Last updated
Get your player objects moving around and synchronized!
Last updated
Now that your player object is properly spawned and prepared, it's time to get it moving. In this section, we'll implement a simple client-authoritative movement script and utilize FishNet's component to effortlessly synchronize that movement across all connected devices.
For many games, particularly those with real-time player input, client-authoritative movement is the most straightforward approach. This means the client directly controls its own player object and then informs the server (and other clients) about its position and rotation.
Client-authoritative movement makes it easier for the client to cheat with speed hacks, teleportation, and other movement related hacks, but it is a lot easier to implement and understand.
Double-click the PlayerMovement
script to open it in your code editor. Replace the default code with the following:
Unlike typical single-player scripts that use MonoBehaviour
, this script inherits from NetworkBehaviour
. This allows for direct access to the IsOwner
field. Although you could still check ownership via the component on a MonoBehaviour, provides significant advantages. It enables , , and (akin to Unity's Start
, Awake
, and OnDestroy
but for networked objects).
Since there will be multiple player game objects in the game, we need to determine which one is "our" local player's one and only move that with our input. The IsOwner
guard clause handles this.
Now we have code that will allow clients to move only their respective player object, but nothing is yet synchronized over the network This means that we won't ever see other players moving, and they won't see us moving.
To fix this, we will use Fish-Networking's built-in component. This component can be used to synchronize a network object's scale, rotation, position, and parent hierarchy.
Open your Player Prefab and add the NetworkTransform component to it.
This component has quite a few settings, but the most important ones for us right now are the Client Authoritative and the Synchronize Position fields. Both of these should be enabled, which will mean positional changes made by the owner client will automatically be synchronized to the server and then to all other clients.
Save your scene and press the Play button in Unity's Editor. Your player capsule should now be visible. Use the W, A, S, D keys or arrow keys to move your player around the scene.
To test multi-player movement, you can build and run the game, which will automatically connect as a client to the editor. You should then be able to control your player in the editor, and the second instance will control its own player, seeing both players move independently.
If using , you should add the Player Input component to your Player Prefab and be sure to disable it
Unlike typical single-player scripts that use MonoBehaviour
, this script inherits from NetworkBehaviour
. This allows for direct access to the IsOwner
field. Although you could still check ownership via the component on a MonoBehaviour, provides significant advantages. It enables , , and (akin to Unity's Start
, Awake
, and OnDestroy
but for networked objects).