Turn-based movement

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!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TurnBased : MonoBehaviour
{
    private enum State { Player1Move, Player2Move, GameOver }

    private State state;

    public GameObject player1;

    public GameObject player2;

    int x;

    // Start is called before the first frame update
    void Start()
    {
        state = State.Player1Move;

        player1 = GameObject.FindGameObjectWithTag("Player");

        player2 = GameObject.FindGameObjectWithTag("Player2");


    }

    // Update is called once per frame
    void Update()
    {
        int x = 0;
       
               
                    if (Input.GetKeyDown(KeyCode.DownArrow) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.RightArrow))
                    {
                        x++;
                    }
            
                    else if (Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.W))
                    {
                        x--;
                    }
  

                if(x == 0)
                {
                  state = State.Player1Move;
                }
                else if (x == 1)
                {
                  state = State.Player2Move;

                }


                switch (state)
                {
                    case State.Player1Move:
                        player1.GetComponent<Player1Movement>().enabled = true;
                        player2.GetComponent<Player2Movement>().enabled = false;
                        break;

                    case State.Player2Move:
                        player1.GetComponent<Player1Movement>().enabled = false;
                        player2.GetComponent<Player2Movement>().enabled = true;
                        break;

                }
  
    

    }
}

Thanks!

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:

    void Start()
    {
        player1 = GameObject.Find("p1");
        player2 = GameObject.Find("p2");
    
        // set initial state
        state = State.Player1Move;
    }

    void Update()
    {
        // Whose turn?
        if (state == State.Player1Move)
        {
            MovePlayer1();
        }
        else
        {
            MovePlayer2();
        }
    }

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.

2 Likes