FishNet icon indicating copy to clipboard operation
FishNet copied to clipboard

Unconsumed GoalDataQueue in NetworkTransform causes a quick jerk when client loses Ownership

Open TsFreddie opened this issue 1 year ago • 3 comments

General Unity version: 2022.3.8f1 Fish-Networking version: 3.11.6R

Description If switching ownership from Client A to Client B while Client A is actively sending transform data, when Client B loses ownership, the object will momentarily goes back to the old position from Client A before update to the correct location.

Replication Here's a simple test script for reproducing

using FishNet.Component.Ownership;
using FishNet.Component.Transforming;
using FishNet.Connection;
using FishNet.Object;
using UnityEngine;

public class TestBehaviour : NetworkBehaviour
{
    private Vector3 _targetPosition;

    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            if (!IsOwner) RequestOwnership();
            if (Camera.main != null)
                _targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            _targetPosition.z = 0;
        }

        if (Input.GetMouseButtonDown(1))
        {
            if (IsOwner) GiveUpOwnership();
        }

        if (IsOwner)
        {
            transform.position = _targetPosition;
            GetComponent<NetworkTransform>().ForceSend();
        }
    }

    [ServerRpc(RequireOwnership = false)]
    public void RequestOwnership(NetworkConnection sender = null)
    {
        GiveOwnership(sender);
    }

    [ServerRpc]
    public void GiveUpOwnership()
    {
        RemoveOwnership();
    }
}

Steps to reproduce the behavior:

  1. Build the project and launch three instances. make one of them the server and the other two clients
  2. Left click anywhere on Client A to grab ownership and update the object's position
  3. Left click anywhere else on Client B to grab ownership from Client A and update the object's position
  4. Right click on Client B to release ownership and observe on Client B

Other Notes

ForceSend was called constantly to simulate constant movement without actually having to control two client at the same time.

Adding a _goalDataQueue.Clear(); at the end of NetworkTransform's SetDefaultGoalData() solved the problem for me, but I'm not sure if keeping the goalDataQueue is intended.

TsFreddie avatar Oct 31 '23 10:10 TsFreddie