Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question about grids on board for Othello game

Discussion in 'Getting Started' started by luyao795, Mar 21, 2016.

  1. luyao795

    luyao795

    Joined:
    Mar 17, 2016
    Posts:
    33
    I have imported and applied the board model and material onto the scene. It looks good. However, I'm still not sure how I can set up those grids in order to determine where those pieces could be placed. Here is the image of my board:
    board.png
    I also have piece model and texture as assets. Right now what I want to do is to make sure that when mouse clicks on one grid, I can place a piece onto that grid (just like what I did on the image except that I placed that piece in that grid manually). I'm thinking about adding a collider onto each grid, but even for this I have no idea what I should do after placing those colliders. Perhaps there is a better way? Or how should I continue what I have done? Looking for your opinions, thanks!
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Just find the point on the board that was clicked, by doing a raycast with a simple box collider. Then simply do the math — you know how wide and tall the board is, and where it's positioned, so it's just a bit of arithmetic to find the row and column.
     
  3. luyao795

    luyao795

    Joined:
    Mar 17, 2016
    Posts:
    33
    The center point of the board is the origin, each grid is a 1x1 square. I'm not sure how to do the detection with raycast. Could you explain in detail a bit more?
    Sorry for the fact that I'm a slow learner. :(
     
  4. maquis

    maquis

    Joined:
    Feb 7, 2016
    Posts:
    61
    For figuring out what was clicked, couldn't you also add an on-click type of method to the square if you made it out of lots of small square objects? If the board is one big object, though, that won't work.

    To keep track of where pieces are, you could create a Board object with a Piece[,] array. That's what I ended up doing for my Tetris game, to track where blocks are. I did orient my tetris board so that the bottom left position for a block would be positioned at (0,0), because that made Board's arithmetic super easy.
     
  5. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    836
    Raycasting is basically just firing a laser in a direction, and then you get information where it hit and what object. In this case, you'll want to fire a ray from the camera from the mouse position (basically straight out the camera wherever the mouse is). This page explains how to do this.

    Now, if your board has a box collider on it, you'll hit the board and get the RaycastHit.point to tell you the coordinates of where you hit. Since you know the board is at the origin, and you know the size of the squares, you can find the correct square that was hit using math.

    upload_2016-3-21_18-46-55.png

    Where the blue dot is the raycast hit point, which I've used x=1.6 and y=1.6 as an example.
     
    JoeStrout likes this.
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Does the board or view ever move? If not you can just convert the mouse position to a board position. As simple as dividing by the size of the screen.
     
  7. luyao795

    luyao795

    Joined:
    Mar 17, 2016
    Posts:
    33
    I will look at that, thanks a lot!

    By the way, does RaycastHit.point return the value in terms of world space? For example, now the origin is at the center of the lower-left grid. And I added a box collider to the board. Now the board position is (3.5, 0, 3.5). Now if RaycastHit hits the center of the lower-left grid, will the coordinate be (0, 0, 0) (or whatever the y value is which is not mattering)?
     
  8. luyao795

    luyao795

    Joined:
    Mar 17, 2016
    Posts:
    33
    No, neither the board nor the camera would move. I moved the board so that the origin now is at the center of the lower-left grid of the board.
     
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    RaycastHit.point is in world space.
     
  10. luyao795

    luyao795

    Joined:
    Mar 17, 2016
    Posts:
    33
    Okay, thanks! I will try that and let you know if I have further questions! :)