#Players postion doesnt sync
1 messages · Page 1 of 1 (latest)
using QFSW.QC;
using System.Collections;
using System.Collections.Generic;
using Unity.Netcode;
using UnityEngine;
public class PlayerNetwork : NetworkBehaviour
{
private NetworkVariable<int> randomNumber= new NetworkVariable<int>(1,NetworkVariableReadPermission.Everyone,NetworkVariableWritePermission.Owner);
public override void OnNetworkSpawn()
{
randomNumber.OnValueChanged += (int previousValue, int newValue) => { Debug.Log(OwnerClientId + " ; " + randomNumber.Value); };
}
private void Update()
{
if(!IsOwner) return;
Vector3 moveDir=new Vector3(0,0,0);
if(Input.GetKeyDown(KeyCode.T))
{
PozServerRpc(this.transform.position,OwnerClientId);
}
if(Input.GetKey(KeyCode.W))
{
moveDir.z = +1f;
}
if (Input.GetKey(KeyCode.A))
{
moveDir.x = -1f;
}
if (Input.GetKey(KeyCode.S))
{
moveDir.z = -1f;
}
if (Input.GetKey(KeyCode.D))
{
moveDir.x = +1f;
}
float moveSpeed = 7f;
transform.position += moveDir * moveSpeed * Time.deltaTime;
}
[ServerRpc]
private void PozServerRpc(Vector3 position,ulong id)
{
if (position != NetworkManager.Singleton.ConnectedClients[id].PlayerObject.transform.position)
Debug.Log("wrong");
GetPozClientRpc();
}
[ClientRpc]
private void GetPozClientRpc()
{
Debug.Log(transform.position + " " + OwnerClientId);
}
}
in the image above you can see that the build that is a client Debugs a different position than the host(Unity editor)
Well if it's a small discrepancy that's probably normal due to latency, that being said just for fun, walk me through your thought process on how the process should work, starting from the point the player gives input
well im using client authority so from the moment the player gives the input the gameobject moves because he is the owner of the gameobject. Then the information is sent to the server and from the server to all the other clients. The problem is that it might look as a small difference here but after a while the difference gets bigger and bigger until clients that share the same camera are looking at two beans that have different positions
@ruby obsidian im gonna ping so you get notification
Yeah thats not quite how you should handle that
The server is the authority, or always should be, your user should send the input to the server and let the server resolve it, alternatively you can calculate the desired movement, and the server should validate if it's valid movement from the servers perspective on the given frame