【问题标题】:if (!localplayer) not doing anythingif (!localplayer) 什么都不做
【发布时间】:2017-04-07 11:00:22
【问题描述】:

我正在制作多人躲避球游戏,每次启动主机和客户端时,只有一个玩家可以移动。

我希望玩家能够独立移动。这是我的(更新的)代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
public class Script4Network : NetworkBehaviour
{

// Use this for initialization
void Start() {
if (!isLocalPlayer)
    {

        gameObject.GetComponent<FirstPersonController>().enabled = false;
        gameObject.GetComponent<throwing>().enabled = false;
        gameObject.GetComponent<HeadBob>().enabled = false;
        // gameObject.GetComponent<Camera>().enabled = false;
    }
}

void Update()
{

}
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    这不是一个答案,因为我已经不团结太久了,但你知道:

    void Update()
    {
        if (!isLocalPlayer)
        {
            return;
        }
    }
    

    什么都不做。与以下内容相同:

    void Update()
    {
        if (!isLocalPlayer)
        {
            return;
        }
        return;
    }
    

    因为每个函数最后都会返回。因此,如果我们不是本地人,请立即返回。否则,立即返回。您需要一些方法来禁用输入控件——也许在Start() 中找到输入控件组件,如果!isLocalPlayer 则禁用。

    【讨论】:

    • 我刚刚尝试了上面的代码,我禁用了控制输入的脚本以及播放器上的其他脚本。结果是它们是独立的,但它们中只有一个可以移动。
    猜你喜欢
    • 1970-01-01
    • 2014-06-20
    • 2014-10-08
    • 1970-01-01
    • 2019-03-08
    • 2016-12-24
    • 2017-04-18
    • 2011-08-03
    • 2014-05-11
    相关资源
    最近更新 更多