- Home /
My Unity multiplayer script does not sync well
Hi, guys. I got trouble implementing my unity multiplayer game. Below is the script attached to the player prefab. The player can move smoothly in both server and host, but the player has glitches in the client. Can someone help me to look at my code below? Thank you.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Netcode;
public class TestPlayerControl : NetworkBehaviour
{
public NetworkVariable<Vector3> position = new NetworkVariable<Vector3>();
public CharacterController characterController;
public void Move()
{
if (IsOwner)
{
Vector3 moveDir = (transform.forward * Input.GetAxis("Vertical"))
+ (transform.right * Input.GetAxis("Horizontal"));
characterController.Move(5 * Time.deltaTime * moveDir);
if (IsServer)
{
position.Value = transform.position;
}
else if (IsClient)
{
MoveServerRpc(transform.position);
}
} else
{
transform.position = position.Value;
}
}
[ServerRpc]
void MoveServerRpc(Vector3 pos)
{
position.Value = pos;
}
// Update is called once per frame
void Update()
{
Move();
}
}
Answer by VanToRiaXVII · May 27 at 08:00 PM
At first glance, you seem to be relying solely on the server to send you the location of the character. If you want to avoid rubberbanding problems, the character movement should always happen on the client and then get validated by the server. That way, there will be no latency between the player input and the movement of the character on the screen.
That said, there could be more issues, so please let me know precisely what you mean by "glitches".
Your answer

Follow this Question
Related Questions
Network Object Under Canvas Impossible ? 1 Answer
General Multiplayer approach 0 Answers
Checking if the other device is out of the application in Network Game 0 Answers
server connecting issue 1 Answer