Moving a piece and changing turns

Moving_Piece

Setup a game board with pieces as explained in this tutorial.

Now make a new script that will handle the logic for moving pieces and swapping turns. In this case:

  • Each player will have one move.
  • A player can move his or her piece to any open space on the board. (This is configured on the logical piece prefab).
  • In each turn, the player selects the piece to move, and then select the square to move the piece to.

The script has to get a reference to the Grid Game Manager, and register the players. It also has to handle mouse input. The setup used for this example is typical of these types of games. We keep track of the currently selected piece. When the grid is clicked:

  • If the currently selected piece is null, and the click is over a piece of the player whose turn it is, we make that piece the selected piece.
  • If it is not null, and we click on an empty square, the piece is moved there (and the turn ends automatically)
  • If it is not null, and we click on a piece of the player whose turn it is, that piece becomes the new currently selected piece.

To check out this example:

  1. Import Abstract Strategy in a new project.
  2. Import the package below into the same project.
  3. Open the MovePieceAndSwapTurns scene.

MovePieceAndSwapTurns.unitypackage

Scroll to Top