Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Turn-based movement

Discussion in '2D' started by jamesschiele, Nov 15, 2019.

  1. jamesschiele

    jamesschiele

    Joined:
    Aug 27, 2019
    Posts:
    1
    Hi all,

    I'm completely new to coding and I'm trying to get a simple two-player turn-based top-down game going where two players share one keyboard.

    Player 1 is currently mapped to the arrow keys and player 2 to the WASD keys. I'd like for each player to be able to move one space at a time in turns.

    My code so far (below) seems to be allowing one player to move rather than the other and I'm stuck on the logic. Any help would be super appreciated!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class TurnBased : MonoBehaviour
    6. {
    7.     private enum State { Player1Move, Player2Move, GameOver }
    8.  
    9.     private State state;
    10.  
    11.     public GameObject player1;
    12.  
    13.     public GameObject player2;
    14.  
    15.     int x;
    16.  
    17.     // Start is called before the first frame update
    18.     void Start()
    19.     {
    20.         state = State.Player1Move;
    21.  
    22.         player1 = GameObject.FindGameObjectWithTag("Player");
    23.  
    24.         player2 = GameObject.FindGameObjectWithTag("Player2");
    25.  
    26.  
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         int x = 0;
    33.        
    34.                
    35.                     if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow))
    36.                     {
    37.                         x++;
    38.                     }
    39.            
    40.                     else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.W))
    41.                     {
    42.                         x--;
    43.                     }
    44.  
    45.  
    46.                 if(x == 0)
    47.                 {
    48.                   state = State.Player1Move;
    49.                 }
    50.                 else if (x == 1)
    51.                 {
    52.                   state = State.Player2Move;
    53.  
    54.                 }
    55.  
    56.  
    57.                 switch (state)
    58.                 {
    59.                     case State.Player1Move:
    60.                         player1.GetComponent<Player1Movement>().enabled = true;
    61.                         player2.GetComponent<Player2Movement>().enabled = false;
    62.                         break;
    63.  
    64.                     case State.Player2Move:
    65.                         player1.GetComponent<Player1Movement>().enabled = false;
    66.                         player2.GetComponent<Player2Movement>().enabled = true;
    67.                         break;
    68.  
    69.                 }
    70.  
    71.    
    72.  
    73.     }
    74. }
    75.  
    Thanks!
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @jamesschiele

    I'm not quite getting what you are aiming for with your movement code, but lets imagine you have a typical turn based two player game, where one player moves using WASD keys and another with arrows. Then you could do something like this:

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         player1 = GameObject.Find("p1");
    4.         player2 = GameObject.Find("p2");
    5.    
    6.         // set initial state
    7.         state = State.Player1Move;
    8.     }
    9.  
    10.     void Update()
    11.     {
    12.         // Whose turn?
    13.         if (state == State.Player1Move)
    14.         {
    15.             MovePlayer1();
    16.         }
    17.         else
    18.         {
    19.             MovePlayer2();
    20.         }
    21.     }
    22.  
    In each player's method:

    1. You take input
    2. If there is some input, only then apply movement to player's position, and change turn to other player.

    This way update can run several frames without input, and turn is only changed when some input is detected.
     
    Last edited: Nov 16, 2019
    jamesschiele and MisterSkitz like this.