Unity + Mirror NetworkTransform 从客户端到服务器

1 篇文章 0 订阅
1 篇文章 0 订阅

开发多人同步项目时,物体Transform信息同步分为两种:

  1. Player(客户端自主控制,服务器广播,其他客户端刷新)
  2. NPC(自身带有AI逻辑,服务器广播其Transform、Animator属性,所有客户端刷新)
  3. 可接力操作物体(如Aclient 将一个物体交给BClient)

前两种情况,网上教程很多,不再赘述。

第三种情况,没有找到相关介绍,而且官网介绍也有一个坑。

Network Transform - Mirrorhttps://mirror-networking.gitbook.io/docs/components/network-transform这里说到

By default, Network Transform is server-authoritative unless you check the box for Client Authority. Client Authority applies to player objects as well as non-player objects that have been specifically assigned to a client, but only for this component. With this enabled, position changes are send from the client to the server.

实践中:场景创建Cube添加NetworkTransform(自动添加NetworkIdentity)勾选ClientAuthority

 打包测试后发现,客户端和服务器都自能各自移动,却不同步位置。

解决方案:仍然勾选该选项,并且在当前操作client请求权限

注意Command请求只能在player上,所以该脚本应该放挂在player上

using Mirror;
using UnityEngine;

public class AuthorityAndOwnerShip : NetworkBehaviour
{
    private GameObject prefab;



    void Update() 
    {
        bool isplayerDead = false;

        if (isplayerDead && base.hasAuthority)
        {
            CmdRequestRespawn();
        }

        RequestOwnerShipOnClick();
    }

    [Command]
    private void CmdRequestRespawn() 
    {
        NetworkServer.Spawn(prefab,GetComponent<NetworkIdentity>().connectionToClient);
    }



    private void RequestOwnerShipOnClick() 
    {
        if (!base.hasAuthority)
        {
            return;
        }

        if (!Input.GetMouseButton(2))
        {
            return;
        }

        RaycastHit hit;
        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out hit,100))
        {
            NetworkIdentity id = hit.collider.GetComponent<NetworkIdentity>();

            if (id != null && !id.hasAuthority)
            {
                CmdRequestAuthority(id);
            }
        }
    }


    [Command]
    private void CmdRequestAuthority(NetworkIdentity id)
    {
        id.RemoveClientAuthority();
        id.AssignClientAuthority(base.connectionToClient);
    }
}

为了验证该效果,在Cube 上挂载一个验证脚本

using Mirror;
using UnityEngine;

public class ChangeColorOnAuthority : NetworkBehaviour
{

    public override void OnStartAuthority()
    {
        base.OnStartAuthority();

        GetComponent<MeshRenderer>().material.color = Color.blue;
    }

    public override void OnStopAuthority()
    {
        base.OnStopAuthority();

        GetComponent<MeshRenderer>().material.color = Color.white;

    }

    void Update() 
    {
        if (Input.GetMouseButton(1))
        {
            transform.Rotate(Vector3.down, Input.GetAxis("Mouse X"));
            transform.Rotate(Vector3.left, -Input.GetAxis("Mouse Y"));
        }
    }
}

链接后,在client上用鼠标中键点击cube、接收到权限后会变成蓝色。此时点击鼠标右键并滑动。旋转角度就会同步回服务器了。

推荐一个clone项目的插件,是以哦那个这个插件就可以不必每次打包才能看同步效果了。

Unity 一键克隆项目工程,双开调试网络同步

注意:

一旦客户端掉线,客户端所拥有的所有物体都会被销毁。

效果视频:

mirror 客户端操作权限

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大只弱鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值