Moving Your Player Around
Get your player objects moving around and synchronized!
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 NetworkTransform component to effortlessly synchronize that movement across all connected devices.
Client-Authoritative Movement Script
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.
Editing the Script
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 NetworkObject component on a MonoBehaviour, NetworkBehaviour provides significant advantages. It enables RPCs, SyncVars, and specialized override methods (akin to Unity's Start
, Awake
, and OnDestroy
but for networked objects).
using FishNet.Object;
using UnityEngine;
// Inherit from NetworkBehaviour instead of MonoBehaviour
public class PlayerMovement : NetworkBehaviour
{
public float moveSpeed = 5f;
void Update()
{
// Only run this code on the object the local client owns.
// This prevents us from moving other players' objects.
if (!IsOwner)
return;
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
Vector3 moveDirection = new Vector3(horizontal, 0f, vertical);
if (moveDirection.magnitude > 1f)
moveDirection.Normalize();
transform.position += moveSpeed * Time.deltaTime * moveDirection;
}
}
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.
Synchronizing the Movement
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 NetworkTransform 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.
Test Player Movement
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.

Last updated