Turn Based Multiplayer

Hi everyone.

l’m really new to unity and programming in general. l’m trying to make a board game in unity, where i hit a button to randomly generate a number between 1 and 6. That number will then move a cube (player 1) along a defined path that many spaces. when that’s finished, it’s player 2’s go at pressing the button for his turn.

however, l’m having a really hard time figuring out how to write a script so that when the first player’s turn ends, the second player’s turn starts. The game is played on one computer, only using the mouse to click the dice roll button.

if someone can please help a complete noob with a script that can help me get the turn based multiplayer working, the rest of the game will be fine.

thanks for your time.


EDIT :

So, i have two cubes at the moment as Ship 1 and Ship 2. Should i put them into prefabs so as the script has somewhere to recognize them from?

Here’s my Boardgame Movement script a friend helped me with yesterday.

public class BoardgameMovement : MonoBehaviour { 

	public Transform target; 
	int diceValue=0; 
	int movementNumber=0; 
	bool test= true; 

	// Use this for initialization 
	void Start () {
	} 

	// Update is called once per frame 
	void Update () 
	{ 
		if(test==true) 
		{ 
			diceValue=2; // the total value of the dice 
			test=false; 
		}

		movementNumber = diceValue; // how many movements remaining

		// while the player can move
		while (movementNumber > 0) 
		{ 
			transform.Translate(-0.01f,0.0f,0.0f); //move forward one unit;

			// if the player reached a corner...
			//
			if(playerPosition = (0,0) || playerPosition = (0,10) || playerPosition = (10,0) || playerPosition = (10,10))
			{
				//rotate player 90 degrees
			}
			
			movementNumber--; 
			Debug.Log (movementNumber); 
			//test=false;
		} 
	}
}

There are quite a few ways of doing this depending on how you have things structured. Here is some pseudo code for one way that will hopefully work well with what you have…

Assuming that you have a game object for each player, and something to store the active player…

GameObject player1;
GameObject player2;
GameObject activePlayer;

Here’s one way to switch the active player…

...
// initialize the active player when starting...

activePlayer = player1;

...

// when checking input...
if (mouse button pressed)
{
  // do the turn over stuff for activePlayer
  TODO :

  // now switch the active player
  if (activePlayer == player1)
    activePlayer = player2;
  else
    activePlayer = player1;

}

So, i have two cubes at the moment as Ship 1 and Ship 2. Should i put them into prefabs so as the script has somewhere to recognize them from?

Here’s my Boardgame Movement script a friend helped me with yesterday.

public class BoardgameMovement : MonoBehaviour {

public Transform target;
int diceValue=0;
int movementNumber=0;
bool test= true;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
	
	if(test==true)
	{
		diceValue=2; // the total value of the dice
		test=false;
	}

movementNumber = diceValue; // how many movements remaining

 

// while the player can move

while (movementNumber > 0)

{
		
	transform.Translate(-0.01f,0.0f,0.0f);
  //move forward one unit;

  // if the player reached a corner...

  //if(playerPosition = (0,0) || playerPosition = (0,10) || playerPosition = (10,0) || playerPosition = (10,10))

  {

    //rotate player 90 degrees

  }

  movementNumber--;
		Debug.Log (movementNumber);
		//test=false;

}
}

}