- Home /
Netcode - client can't move his player.,Unity netcode movement not working on clients
So i've followed the hello world page and the golden path page from the Netcode documentation and i wanted to create a simple project where multiple people join and move a cube around. The problem is when i join as a host i can move my cube but when someone else joins as a client it cannot move his cube. The host cube's transform is synced, the clients can see him moving but they cant move at all. I've found out that if i disable network transform component it will let me move the cube as a client but the transform will not be synced so i'm not sure what could be the problem...
here is the code for the movement script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
public class PlayerMovement : NetworkBehaviour
{
public float movementSpeed = 5f;
public CharacterController controller;
void Start()
{
controller = GetComponent<CharacterController>();
transform.position = new Vector3(0f, 0.6f, 0f);
}
void Update()
{
if (IsLocalPlayer)
{
Move();
}
}
void Move()
{
Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
input = Vector3.ClampMagnitude(input, 1f);
Vector3 movement = transform.TransformVector(input) * movementSpeed;
controller.Move(movement * Time.deltaTime);
}
}
here is the prefab i'm currently using
Seeing the same issue. Recreated and redone project multiple times with no success.
Answer by Lozzzyy · Nov 17, 2021 at 12:52 PM
TLDR: Go to Package Manager->Netcode for GameObjects->Samples->Import ClientNetworkTransform. That gives you a client-authoritative network transform component.
Just been having this issue myself. From the NetworkTransform documentation page (https://docs-multiplayer.unity3d.com/docs/components/networktransform) :
> NetworkTransform always synchronizes positions from the server to the clients and position changes on the clients are not allowed. Netcode for GameObjects comes with a sample containing a ClientNetworkTransform. This transform synchronizes the position of the owner client to the server and all other client allowing for client authoritative gameplay.
So when you use the default NetworkTransform component it only synchronises the transform from Server -> to Client, meaning it doesn't take the client into account at all. Your client's movement inputs are being nullified because on the server end your object isn't moving (i.e. server authority), if that makes sense. I replaced NetworkTransform with the ClientNetworkTransform you can get from the package manager and it worked as I expected.
My previous experience with Unity multiplayer was using Mirror which has a client authority option built into its NetworkTransform (I think it was the default setting?). As far as I can tell the entire point of the component is a convenient way to sync transforms of things like players, so I'm confused why the default component on Unity's official solution is server authoritative with no options.
How would i setup this in a way where the client would get that script rather than the network transform script
Trusting the client is a bad design because it makes cheating a lot easier. Valheim does this, for example, you can make your character jump 10 times higher than it's supposed to and it will be visible to anyone on the server. There are ways to keep server authority over player position (and other attributes) and still keep the movement smooth. See: https://www.gabrielgambetta.com/client-server-game-architecture.html.
Your answer
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Best way to choose Google Play Realtime Multiplayer Host? 0 Answers
Network command is running on client? 1 Answer
RPC and Command Calls OR Network messages for syncing variables across Server and Client? 0 Answers
When to process input in networked multiplayer game? 0 Answers