简体   繁体   English

Unity 中的镜像网络问题

[英]Problems with Mirror Networking in Unity

I'm relatively new to Unity, and decided to make a simple multiplayer game.我对 Unity 比较陌生,并决定制作一个简单的多人游戏。 After implementing the multiplayer aspect, two main problems arose.在实现多人游戏方面后,出现了两个主要问题。 One, every person that joins will have n times the speed that they are supposed to have, where n is the number of players after the player joined.一,每个加入的人都将拥有他们应该拥有的 n 倍的速度,其中 n 是玩家加入后的玩家数量。 Two, I have a very simple camera script where it disables a camera if it isn't tied to the local player and enables it if it is.第二,我有一个非常简单的相机脚本,如果相机没有绑定到本地播放器,它会禁用它,如果它是,它会启用它。 This doesn't work however, as everyone who joins can't connect to any camera and the host rapidly switches between the cameras.但是,这不起作用,因为加入的每个人都无法连接到任何摄像机,并且主机会在摄像机之间快速切换。

Here is the code:这是代码:

using Mirror;
using UnityEngine;

public class PlayerObject : NetworkBehaviour
{

    public GameObject playerPrefab;
    public KeyCode left;
    public KeyCode right;
    public float speed;
    GameObject myPlayerUnit;

    void Start()
    {
        if (isLocalPlayer == false)
        {
            return;
        }

        //Instantiate(playerPrefab);
        CmdSpawn();
        Debug.Log(myPlayerUnit == null);
    }

    void Update()
    {
        FindObjectOfType<Camera>().enabled = false;
        if (isLocalPlayer == false)
        {
            return;
        }
        CmdSetCamera(true);
        if (Input.GetKey(left))
        {
            CmdMove(false);
        }

        if (Input.GetKey(right))
        {
            CmdMove(true);
        }
    }
    [Command]
    void CmdSpawn()
    {
        GameObject go = Instantiate(playerPrefab);
        myPlayerUnit = go;
        NetworkServer.Spawn(go);
    }

    [Command]
    void CmdSetCamera(bool en)
    {
        if (myPlayerUnit != null)
        {
            myPlayerUnit.GetComponent<Camera>().enabled = en;
        }
    }

    [Command]
    void CmdMove(bool right)
    {
        if (myPlayerUnit == null)
        {
            return;
        }

        if (right)
        {
            myPlayerUnit.transform.Translate(speed, 0, 0);
        }
        else
        {
            myPlayerUnit.transform.Translate(-speed, 0, 0);
        }
    }
}

Before you spend more time editing and trying to fix this code, I would scrap it and try a new simpler approach.在您花更多时间编辑和尝试修复此代码之前,我会废弃它并尝试一种新的更简单的方法。 One of the main problems with the method you chose here is that you are starting every frame by shutting the camera off " FindObjectOfType<Camera>().enabled = false; ", and the camera enabling.disabling and switching is needlessly complicated.您在此处选择的方法的主要问题之一是您通过关闭相机“ FindObjectOfType<Camera>().enabled = false; ”开始每一帧,并且相机启用.禁用和切换是不必要的复杂。

I don't have time to give a code example right now, but Mirror works, so a simpler method will be easier to debug for you, and make sure to include Debug.Log statements at every part so that you know what's happening at every step.我现在没有时间给出代码示例,但是 Mirror 可以工作,因此更简单的方法会更容易为您调试,并确保在每个部分都包含 Debug.Log 语句,以便您知道每个部分发生了什么步。

For the speed it looks like you are calculating it based on the player unit, and since the player units are messing with each other as you described with the cameras so you would want todo something like this对于速度,您似乎是根据播放器单元计算它,并且由于播放器单元相互混淆,正如您对相机所描述的那样,所以您想要做这样的事情

void Start() {
speed = 10f; //you need to define what your speed is or unity will calculate it weirdly
}

For the camera issue you should probably have the camera disabled in the prefab and enable it when the player joins对于相机问题,您可能应该在预制件中禁用相机并在玩家加入时启用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM