新客户更改权限统一镜像

时间:2021-06-01 13:16:26

标签: authorization multiplayer mirror

我目前正在开发一款 3d 统一游戏,并尝试使用镜像使其成为多人游戏。我已经设置好游戏并且一切都在独立的相机移动和动作下顺利运行,但是当我尝试制作相机脚本以附加到播放器预制手电筒(如下所示)时,它遇到了错误: 第一个托管服务器的人拥有完全权限并且能够切换摄像头。然而,它只切换每个玩家单独的相机。这不是什么大问题,我可以轻松解决这个问题。问题是加入服务器的第二个及以后的客户端没有权限,当我尝试切换脚本时,它警告我没有权限的对象正在尝试使用命令,并且没有执行。 有没有办法确保大厅中的每个玩家都有权限? 在堆栈溢出上有一个类似的问题,但它从未得到回答,所以我在这里重新措辞。 手电筒代码:

// Turns the light component of this object on/off when the user presses and releases the `L` key.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Mirror;

public class FlashlightToggle : NetworkBehaviour
{

    public Light lightaffected;

    void Start()
    {
        //get the localplayer's light, where the small bug comes in don't worry about it
        if (!isLocalPlayer) return;
            lightaffected = GetComponentInChildren<Light>();
    }

    void localToggleLight()
    {
        //just toggle the light is all
        lightaffected.enabled = !lightaffected.enabled;
    }

    [Command]
    void CmdToggleLight()
    {
        //Apply it to all other clients
        localToggleLight();
        RpcToggleLight();
    }

    [ClientRpc]
    void RpcToggleLight()
    {
        //toggle locally
        if (!isLocalPlayer) return;
        localToggleLight();
    }

    void Update()
    {
        // Toggle light on/off when L key is pressed.
        // Test if Local Client or Another


            if (Input.GetKeyUp(KeyCode.L))
            {
                if (isServer)
                {
                    RpcToggleLight();
                }
                else
                {
                    CmdToggleLight();
                }
            }
        
    }
}

我为格式道歉

0 个答案:

没有答案