简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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