【问题标题】:Unity/Mirror Network Delay After Transferring Client Authority转移客户端权限后的 Unity/Mirror 网络延迟
【发布时间】:2021-12-31 17:56:42
【问题描述】:

这是一个复杂的问题,所以我附上了一个 gif 来展示 what's going on

事件顺序:

  1. 玩家 B(右侧)正在控制人类并拥有客户端权限
  2. 玩家 A(左侧)用书架击打人类
  3. 玩家 B 从人类身上移除,变回粉红色球,并移除人类的客户端权限
  4. 玩家 A 被分配了人类的客户端权限
  5. 当玩家 A 离开时,人体模型在局部与玩家 A 同步移动(忽略粉球在控制人类时不会渲染,这是一个特征)
  6. 从玩家 B 的角度来看,人体模型慢慢接近粉球(玩家 B),而客户端缓冲区和客户端追赶最大。
  7. 最后,人类追踪玩家 A 的路径,直到它最终追上并保持同步。

一些注意事项:

  • 前几次权限切换,不会报错
  • 每次权限转移后效果会变长,最终会导致 Unity 崩溃
  • 人类预制件的“hasAuthority”布尔值似乎可以正确打开和关闭

Network Transform for Human Prefab

玩家A调用CmdControlPlayer函数,'other'是玩家B当前控制的人:

[Command]
public void CmdControlPlayer(GameObject other)
{        
    //Unrelated code

    AssignAuthority(other);

    //Unrelated code
}

void AssignAuthority(GameObject other)
{
    RemoveClientAuthority(other);

    other.GetComponent<NetworkIdentity>().AssignClientAuthority(connectionToClient);
}

void RemoveClientAuthority(GameObject other)
{
    if(other.GetComponent<NetworkTransform>().connectionToClient != null)
    {
        other.GetComponent<NetworkIdentity>().RemoveClientAuthority();
    }
}

输入被转换为运动如下:

private Vector3 bodyMovement;
public GameObject possessedObject;
[SerializeField] private Rigidbody controllableBody;

//Called in Update function
void PlayerInput()
{
    float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
        
    input_X = Input.GetAxisRaw("Horizontal");
    input_Z = Input.GetAxisRaw("Vertical");

    // If the player is controlling the human
    if(isHuman)
    {
        bodyMovement = (controlledObject.transform.right * input_X) + (controlledObject.transform.forward * input_Z);
        controlledObject.transform.Rotate(Vector3.up * mouseX);         
    }
    else
    {
        //Control pink ball
    }
}

void FixedUpdate()
{
    if(hasAuthority)
    {
        if(controlledObject != null)
        {
            transform.position = controlledObject.transform.position;

            if(isHuman)
            {
                transform.rotation = controlledObject.transform.rotation;

                // RigidBody Movement For Human //
                controllableBody.velocity = ((bodyMovement.normalized * moveSpeed) + (transform.up * controllableBody.velocity.y));
                Camera.main.transform.localRotation = Quaternion.Euler(yRotation, 0f, 0f);                    
            }
        }
    }
}

我的猜测是,这与缓冲区充满了一些东西但在短时间内无法赶上有关。欢迎任何帮助!

在 Windows 10 上使用 Unity 2020.3.20f1 Personal

【问题讨论】:

  • 有人对此有任何想法吗?

标签: c# unity3d mirror


【解决方案1】:

我已经在“播放器”中测试了切换权限,在做了很多次之后,我开始注意到一些延迟。清除Unity下任务管理器中所有无用的任务进程,恢复正常。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 2020-12-15
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    相关资源
    最近更新 更多