Chess Game Tutorial • 1/5 • [Tutorial][C#] - creating3dgames.com

Chess Game Tutorial • 1/5 • [Tutorial][C#]

N3K EN
Views: 156498
Like: 1462
In this first episode, we lay down the board and spawn all our pieces using a manager script that will control the flow of the game.

Project files can be found here:

The assets, here :

►——————————————————-◄
Join the community, keep on learning.

► Come hang out in discord! This is the most efficient way to reach not only the team members but thousands of other developers like you! We love it when you help, showcase or just discuss with each other in discord!

► Learn about unity and game design with this playlist:

► If you enjoy what I do here and would like to support me, pledge any amount on Patreon.

►——————————————————-◄
Stay up to date!

N3K is friendly, laidback community that helps you create video games, and make friends while doing so. Check out our website to learn more, or join us on discord!

► Keep up to date with our plans! Follow the Facebook page

►——————————————————-◄
More content!

Do you need some ideas?
Try out our Unity Training playlist!

►Subway Surfer, mobile game:
►Glide, Mobile game Tutorial:
►Multiplayer Checkers Tutorial:
►2.5D Platformer Tutorial (Noob friendly!):
►Unity Mobile Game (Roller Ball):
►Endless Runner:
►Chess Game:

112 Comments

  1. how to rotate just a black knight ?
    (private List<GameObject> orientation = Quaternion.Euler(0, 180, 0);)

  2. I don't know if I'll run into any issues later down the line, but I have fixed the rotation issues with a boolean to check if my piece is black or white like so:

    private void SpawnChessPiece(int index, Vector3 position, bool white)
    {
    if (white)
    {
    GameObject go = Instantiate(chessPiecePrefabs[index], position, Quaternion.identity) as GameObject;
    go.transform.SetParent(transform);
    activePiece.Add(go);
    }
    else
    {
    GameObject go = Instantiate(chessPiecePrefabs[index], position, orientation) as GameObject;
    go.transform.SetParent(transform);
    activePiece.Add(go);
    }
    }

    private void SpawnAllPieces()
    {
    activePiece = new List<GameObject>();

    // Spawn team white
    SpawnChessPiece(0, GetTileCenter(4, 0), true); // King
    SpawnChessPiece(1, GetTileCenter(3, 0), true); // Queen
    SpawnChessPiece(2, GetTileCenter(0, 0), true); // Left Rook
    SpawnChessPiece(2, GetTileCenter(7, 0), true); // Right Rook
    SpawnChessPiece(3, GetTileCenter(2, 0), true); // Left Bishop
    SpawnChessPiece(3, GetTileCenter(5, 0), true); // Right Bishop
    SpawnChessPiece(4, GetTileCenter(1, 0), true); // Left Knight
    SpawnChessPiece(4, GetTileCenter(6, 0), true); // Right Knight

    for (int i = 0; i < 8; i++)
    SpawnChessPiece(5, GetTileCenter(i, 1), true); // Pawns

    // Spawn team black
    SpawnChessPiece(6, GetTileCenter(3, 7), false); // King
    SpawnChessPiece(7, GetTileCenter(4, 7), false); // Queen
    SpawnChessPiece(8, GetTileCenter(7, 7), false); // Left Rook
    SpawnChessPiece(8, GetTileCenter(0, 7), false); // Right Rook
    SpawnChessPiece(9, GetTileCenter(5, 7), false); // Left Bishop
    SpawnChessPiece(9, GetTileCenter(2, 7), false); // Right Bishop
    SpawnChessPiece(10, GetTileCenter(6, 7), false); // Left Knight
    SpawnChessPiece(10, GetTileCenter(1, 7), false); // Right Knight

    for (int i = 0; i < 8; i++)
    SpawnChessPiece(11, GetTileCenter(i, 6), false); // Pawns

    }

  3. hello bro, how do we add Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition) to android
    pls help me
    Is that input.gettouch(0).position?

  4. i made this little number to speed up spawning chess pieces i left the king and queen out and set the others to a for each loop i allso shuffled my peaces around a bit so it goes like a chess set
    // king
    SpawnChessMen(3, getTileCenter(3, 0));
    SpawnChessMen(10, getTileCenter(3, 7));

    //queen
    SpawnChessMen(4, getTileCenter(4, 0));
    SpawnChessMen(9, getTileCenter(4, 7));

    int D = 7;
    for(int W = 0; W<3; W++)
    {
    //white team
    SpawnChessMen(W, getTileCenter(W, 0));
    SpawnChessMen(W, getTileCenter(D, 0));
    // black team
    SpawnChessMen(W + 6, getTileCenter(W, 7));
    SpawnChessMen(W + 6, getTileCenter(D, 7));
    D -= 1;
    }
    for(int C = 0; C<8;C++)
    {
    SpawnChessMen(5, getTileCenter(C, 1));
    SpawnChessMen(11, getTileCenter(C, 6));
    }

  5. On the picture of the chess assets on the asset store, they have laid out the pieces wrongly

  6. This is by far the best tutorial for unity i have seen, compact and precise, im a beginner so take more time in grasping the logic but still it helped me, i hope to make an android game some day.

  7. In your for loops to draw the board, aren't you drawing the 8 vertical lines 8 times?

  8. sir please reply me quickely..sir i am working in visual studio and when i make board .my board does,t access control over board.when i scrooll mouse over square of chess .no reaction occur ,,please help me sir

  9. Here is the proper working code with rotation fix :

    using System.Collections;

    using System.Collections.Generic;

    using UnityEngine;

    public class BoardManager : MonoBehaviour {

    private const float TILE_SIZE = 1.0f;

    private const float TILE_OFFSET = 0.5f;

    private int selectionX = -1;

    private int selectionY = -1;

    public List<GameObject> chesspiecePrefabs;

    private List<GameObject> activeChesspiece;

    private Quaternion blackRot = Quaternion.Euler(0, 180, 0);

    private Quaternion whiteRot = Quaternion.identity;

    private void Start()

    {

    SpawnAllChesspieces();

    }

    void Update () {

    UpdateSelection();

    DrawChessBoard();

    }

    private void DrawChessBoard()

    {

    Vector3 widthLine = Vector3.right * 8;

    Vector3 heightLine = Vector3.forward * 8;

    for (int i = 0; i<= 8; i++)

    {

    Vector3 start = Vector3.forward * i;

    Debug.DrawLine(start, start + widthLine);

    for (int j = 0; j <= 8; j++)

    {

    start = Vector3.right * j;

    Debug.DrawLine(start, start + heightLine);

    }

    }

    //draw the selection line

    if(selectionX >=0 && selectionY >=0)

    {

    Debug.DrawLine(

    Vector3.forward * selectionY + Vector3.right * selectionX,

    Vector3.forward * (selectionY + 1) + Vector3.right * (selectionX + 1));

    Debug.DrawLine(

    Vector3.forward * (selectionY + 1) + Vector3.right * selectionX,

    Vector3.forward * selectionY + Vector3.right * (selectionX + 1));

    }

    }

    private void SpawnAllChesspieces()

    {

    activeChesspiece = new List<GameObject>();

    //spawn the white pieces

    //king

    SpawnChesspiece(0, GetTileCenter(3, 0), whiteRot);

    //queen

    SpawnChesspiece(1, GetTileCenter(4, 0), whiteRot);

    //rook

    SpawnChesspiece(2, GetTileCenter(0, 0), whiteRot);

    SpawnChesspiece(2, GetTileCenter(7, 0), whiteRot);

    //bishop

    SpawnChesspiece(3, GetTileCenter(2, 0), whiteRot);

    SpawnChesspiece(3, GetTileCenter(5, 0), whiteRot);

    //knight

    SpawnChesspiece(4, GetTileCenter(1, 0), whiteRot);

    SpawnChesspiece(4, GetTileCenter(6, 0), whiteRot);

    //pawn

    for (int i = 0; i < 8; i++)

    {

    SpawnChesspiece(5, GetTileCenter(i, 1), whiteRot);

    }

    //spawn all the black pieces right now

    //king

    SpawnChesspiece(6, GetTileCenter(3, 7), blackRot);

    //queen

    SpawnChesspiece(7, GetTileCenter(4, 7), blackRot);

    //rook

    SpawnChesspiece(8, GetTileCenter(0, 7), blackRot);

    SpawnChesspiece(8, GetTileCenter(7, 7), blackRot);

    //bishop

    SpawnChesspiece(9, GetTileCenter(2, 7), blackRot);

    SpawnChesspiece(9, GetTileCenter(5, 7), blackRot );

    //knight

    SpawnChesspiece(10, GetTileCenter(1, 7), blackRot);

    SpawnChesspiece(10, GetTileCenter(6, 7), blackRot);

    //pawn

    for (int i = 0; i < 8; i++)

    {

    SpawnChesspiece(11, GetTileCenter(i, 6), blackRot);

    }

    }

    private void UpdateSelection()

    {

    if (!Camera.main)

    return;

    RaycastHit hit;

    if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 25f, LayerMask.GetMask("ChessPlane")))

    {

    selectionX = (int)hit.point.x;

    selectionY = (int)hit.point.z;

    }

    else

    {

    selectionX = -1;

    selectionY = -1;

    }

    }

    private void SpawnChesspiece(int index, Vector3 position, Quaternion rot)

    {

    GameObject go = Instantiate(chesspiecePrefabs[index], position, rot) as GameObject;

    go.transform.SetParent(transform);

    activeChesspiece.Add(go);

    }

    private Vector3 GetTileCenter(int x, int y)

    {

    Vector3 origin = Vector3.zero;

    origin.x += (TILE_SIZE * x) + TILE_OFFSET;

    origin.z += (TILE_SIZE * y) + TILE_OFFSET;

    return origin;

    }

    }

  10. Topsoil Depletion Awareness (closing the loop) says:

    how would I make a 3d grid?

  11. Topsoil Depletion Awareness (closing the loop) says:

    Can I have something like SpawnChessman(int index, Vector3 position, orientation)? What kind of value is orientation?

  12. Topsoil Depletion Awareness (closing the loop) says:

    This doesnt work:
    if (selectionX >= 0 && selectionY >= 0) { //draw x
    print("x");
    Debug.DrawLine(
    Vector3.forward * selectionY + Vector3.right * selectionX,
    Vector3.forward * (selectionY + 1) + Vector3.right * (selectionX + 1));
    }

  13. i can see grid lines in game window but i can't interact with the grid lines just like shown in 19:10 …. while i have followed the steps exactly as shown in the video.

  14. OK, I am finally able to follow along again, but to prevent others from making the same mistakes: I had Raycast hit; instead of RaycastHit hit; This cause the error: "hit may not be used before it has been initialized, and I got an "ambiguous reference" error on Debug.log. This can be solved by deleting the using using System.Diagnostics at the top.

  15. Also at time 13:50 in the video you SPEAK "Layer.getMask("ChessPlane")", but you type: LayerMask("ChessPlane"), the later is the correct one. I also was missing the quotes for a moment and thought that I would have to declare and supply the gameObject to the script. OK… on I go!

  16. For people struggling with the orientation, there is a simple fix.

    Firstly set the orientation y value to 0 so it's (0,0,0).

    In the SpawnAllChessmans function after spawning your first set of chess pieces change the value of orientation to 180.

    (orientation.y = 180f;)

    This works visa versa depending on whether or not it is you black pieces that are incorrect or the white pieces.

  17. Hey, i can't get unity to draw the debug line and i've made sure gizmos are on. I'm using Unity 2018.3.8f1. can anyone help?

  18. Awesome! I will use this tutorial to do some MOBA chess hahaha

  19. those bars are visible only in scene not in game
    edit –
    gizmos should be pressed in both game and scene screen.

  20. x can not be see
    edit –
    You have to set the main camera tag on Main Camera

  21. if your horse is not getting set in right position then instead of 180, try 18, worked for me –

    private Quaternion orientation = Quaternion.Euler(0,18,0);

  22. I know this is an old tutorial but if you were having an issue with the Vector3 Intellisense in Visual Studio there is a fix. Go to Edit>Preferences>External Tools>External Script Editor and change it to Visual Studio using the Browse button. Reload or recreate the project and it works. Also if you are unable to see the lines you have to turn on GIZMOS in the top right of the Game view.

  23. 1:11 Game object chessboard is visible in scene tab,but not in the game tab

  24. I love your tutorial on this, although if you could update the file for the project files, that'd be amazing

  25. Gizmos need to be enabled in the GAME view, not the SCENE view like I was looking at for the white grid to be visible further along in the video (just like Cian Brennan's comment – I didn't see his comment for a few hours sigh

  26. This was a very solid tutorial. I was searching YouTube for a good video (or series) to watch that was informative and actually looked like the person was good at what they were doing. Keep up the great work!

  27. Hussam essmahi -حسام السماحي says:

    the vector in my visuel studio2013 cant be green why??

  28. hey , i hope you can respond to this thanks. When i press on play , the game camera do not show anything , but in the scene i do see the draw lines. When i put the render on the plane , it does show in the game camera. Ca you explain me why ? The link to the project isnt available anymore , can you give me the link ?

  29. Hi. I cant re-orient teh white knightes with quaternion euler thing… Can someone help? Thanks

  30. I stumbled on your videos few times. Great work but there is one thing that triggers me. Pronounciation of word MANAGER

  31. For anyone not seeing the grid, you need to follow the step at 7:50 and actually add DrawChessBoard into the void Update routine. Then it works. Spent a few hours figuring this out! Hope it works for you

  32. The project files link is broken! Please help

  33. Hey! Wonderful video ❣️ Could you tell how can I make the game using Ai or something so that we can play against CPU and it's moves are also not fixed. Like a chess bot or something???

  34. The assets, here: https://www.assetstore.unity3d.com/#!...
    Can we download it for free?

    I tried with other free Prefabs but not working. What to do?
    At the 33min of the video where you are playing the game and Prefabs is showing on Game display, that is not happening for me. Prefabs are not showing 🙁

    Completly stuck there. Pls pls pls help me out…

  35. For all the people who wanna do it the correct way. The King is on GetTileCenter 4.0 And the Queen on GetTileCenter 3.0. But very nice video. Thanks a lot!

Leave a Reply

Your email address will not be published. Required fields are marked *