简体   繁体   English

在Unity 5中如何在多人游戏开始之前设置计时器?

[英]How to make timer before multiplayer game start in unity 5.?

Here I am trying to create a multiplayer game. 在这里,我试图创建一个多人游戏。 I want to put timer before game start so, I do research, but can't get any solution for this. 我想在游戏开始前设置计时器,所以我进行了研究,但对此没有任何解决方案。 I am currently using "Coroutine" for this. 我目前正在为此使用“协程”。 but not working.it show different time for all players. 但无法正常工作。它为所有玩家显示了不同的时间。

private int j = 10;

void Start()
{
    if (isLocalPlayer)
    StartCoroutine (GameStartTimer ());
}


IEnumerator GameStartTimer()
{
    while (j > 0) {
        j = j - 1;
        Debug.Log ("Value of j is : " + j);
        startTimertext.text = j.ToString ();
        yield return new WaitForSeconds (1);
    }
    //ObjectParentingSystem ();
    GetPlayerAnimandRGB ();
    StartCoroutine (StartPlayerTimer ());
    //yield return new WaitForSeconds (10);
    gameStartTextPanel.SetActive (false);
}

I also try below code but, can't get succes. 我也尝试下面的代码,但不能成功。

void Start()
{
    if (isServer){
        StartCoroutine (GameStartTimer ());   
    }
    else{
        GetPlayerAnimandRGB ();
        StartCoroutine (StartPlayerTimer ());
        //yield return new WaitForSeconds (10);
        gameStartTextPanel.SetActive (false);
    }
}

If any one have code for this or tutorials then pls share link. 如果任何人有此代码或教程的代码,则请共享链接。

If I understand this right, you've a wait timer that keeps ticking for 10 seconds. 如果我理解这一权利,则您有一个等待计时器,该计时器持续滴答10秒钟。 Once this is zero, the start game timer begins and only those connected can play the game. 一旦为零,则开始游戏计时器开始计时,只有连接的那些人才能玩游戏。

To keep all players and timers in sync you will have to either designate one client as the server responsible for keeping all other clients in sync or have a server broadcast the time to all players who connect to the room. 为了使所有玩家和计时器保持同步,您必须指定一个客户端作为负责使所有其他客户端保持同步的服务器,或者让服务器向连接到该房间的所有玩家广播时间。 Without this, you will not be able to keep the time in sync as players don't join the room at the same time. 否则,您将无法保持时间同步,因为玩家不会同时加入游戏室。

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

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