#Client is moving faster than host

1 messages · Page 1 of 1 (latest)

brazen stream
#

New to multiplayer, I am using Unity 6 and using multiplayer tools 2nd player to test this.
I have tried using both Time.Deltatime and FixedDeltatime.

using System.Collections;
using Unity.Netcode;
using UnityEngine;

public class Test_Movement : NetworkBehaviour
{
    public float movementSpeed = 10f;
    private Vector2 movementDirection;

    // Update is called once per frame
    void Update()
    {
        if (!IsOwner) { return; }
        input();
    }

    private void input()
    {
        float X = Input.GetAxis("Horizontal");
        float Y = Input.GetAxis("Vertical");
        MovementRPC(X, Y);
    }

    [Rpc(SendTo.Server)]
    void MovementRPC(float X, float Y)
    {
        if (!IsServer) return;
        movementDirection = new Vector2(X, Y).normalized;
        transform.Translate(movementDirection * movementSpeed * Time.fixedDeltaTime);
    }

}

public class TestGameManager : NetworkBehaviour
{
    public List<Transform> spawnPoint;
    public GameObject playerPrefab;
    public override void OnNetworkSpawn()
    {
        base.OnNetworkSpawn();

        if (IsServer)
        {
            NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnected;
        }
    }

    void OnClientConnected(ulong clientId)
    {
        var spawnedPlayer = Instantiate(playerPrefab, spawnPoint[((int)clientId)].position, Quaternion.identity);
        spawnedPlayer.GetComponent<NetworkObject>().SpawnWithOwnership(clientId);
    }
}
plain vortex
#

You are using Time.fixedDeltaTime in update

#

you need to use Time.deltaTime

brazen stream
#

I tried using both, the result is same.

plain vortex
#

Could be packet loss or throttling

What you should do is just sending movement state to the server

Tell the server what the current inputs are and it should update the position itself, dont send the movement every frame

#

if movement values are the same dont send anything, if you stop inputting any movement send 0, 0 to tell the server you stopped and then just run the Update on both sides

brazen stream
#

I will try it and update you in a few minutes.

plain vortex
#

Alright, that should also drastically lower packet amounts

You can later expand on this concept with movement prediction and error compensation, but start simple 😄

brazen stream
#

Yes, this fixed it.
So basically we have to not spam send inputs to server? just send when something changes?

  void Update()
    {
        if (!IsOwner) return;

        // 1. Gather Input
        Vector2 newInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")).normalized;

        // 2. Send only when input changes
        if (newInput != lastSentInput)
        {
            lastSentInput = newInput;
            SubmitInputServerRpc(newInput);
        }
    }

    [ServerRpc]
    void SubmitInputServerRpc(Vector2 input)
    {
        currentInput = input; // Store this player's current direction
    }

    void FixedUpdate()
    {
        // 3. Only the server moves players (authoritative)
        if (IsServer && currentInput != Vector2.zero)
        {
            transform.Translate(currentInput * movementSpeed * Time.deltaTime);
        }
    }
dense crown
#

You probably need to lock the frame rate.

plain vortex
# brazen stream Yes, this fixed it. So basically we have to not spam send inputs to server? jus...

Yes, generally just sending changes, called "deltas" is the better approach unless you want very precise data it also saves bandwith and computing power
The method you now used is prone to desyncs when you drop packets or the simulation is just a hair different
So you usually add checks every n-seconds / on specific events if the server has the same position as the client and if the distance is too big the client snaps or corrects to the server position

brazen stream
#

Got it, Thank you Sidia. really appreciate it!

#

I will be bugging you more since m learning this 😛

plain vortex
#

btw how do you currently move the client? i only see a transform.Translate for the server now

#

or are you using networked transforms?

brazen stream
#

The NetworkTransform Script is attached to players

plain vortex
#

ohh

#

then you dont need to send it at all 😄

#

as the owner just move the client entity

#

unity will do the rest for you

brazen stream
#

so i just move the client and everything is automatically sent to server?

plain vortex
#

im not sure how its currently setup, maybe you need to set a sync mode on the script but it should, yes

brazen stream
#

Its just the default.
but then I was confused because there is no server checking, shouldnt server be the one moving players?

plain vortex
#

if you want a full authorative approach, yes

brazen stream
#

in that approach we dont use networkTransform? and do it using script like I did?

plain vortex
#

I am not sure if unity has a solution for that built in, you would need to research, otherwise yes

#

I'm not too familiar with unity's internal network stuff cause I am using third party network libraries

brazen stream
#

Okay.
I will just experiment around and see

#

Can i dm you if i get have some issues? i wont bother you unless I get answers from internet and chatGPT

plain vortex
#

better just ping me in here

brazen stream
#

that works too. thank you sir.

plain vortex
#

i dont always see my dms, I own a rather large discord server and they get a bit crowded from time to time 😄

brazen stream
#

Oh okay. understandable.

dense crown