network-programmingunity-game-enginemultiplayeronline-game

How to change the turn of a player in an online multiplayer game in Unity


I'm new in Unity and I'm trying to create an online multiplayer game. This game is a tanks fight. The tank is prefab and it's called to be created twice. When you press the Space keyCode the tank shoots a bullet. I have all the network connections done but now I want to control the players turn. For example, if localPlayer shoots one time this would have to be the turn of the second player and so on.

Can anyone help me please?


Solution

  • How about having a common variable which updates after each turn. depending on the value of the variable you can determine who has the turn. you have to define the order which they take turns before starting the game. Assume you have n players, and a common int value x which increases by 1 after each turn.

    if( x%n == turn && Input.GetKeyDown(KeyCode.Space))
      shoot();
    

    players will not be able to shoot unless it's their turn. use a simple RPC function to update x from clients after each turn.