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

I cant callculate take right tile from my grid

Discussion in '2D' started by shyxiaolong, Sep 6, 2022.

  1. shyxiaolong

    shyxiaolong

    Joined:
    Jan 14, 2021
    Posts:
    9
    i have some problem.
    my script board
    Code (CSharp):
    1. private GameTile[,] board;
    2.     private Vector2 offset;
    3.  
    4.     [SerializeField] float cellWidth;
    5.     [SerializeField] float cellHeight;
    6.     [SerializeField] int countX;
    7.     [SerializeField] int countY;
    8.     [SerializeField] GameObject background;
    9.  
    10.     private float boardWidth;
    11.     private float boardHeight;
    12.  
    13.     public void CreateBoardCells()
    14.     {
    15.  
    16.         float worldHeightScreen = Camera.main.orthographicSize * 2f;
    17.         float worldWidthScreen = worldHeightScreen / Screen.height * Screen.width;
    18.  
    19.         cellWidth = worldWidthScreen / (countX + 2);
    20.         cellHeight = worldHeightScreen / (countY + 6);
    21.  
    22.         boardWidth = cellWidth * countX;
    23.         boardHeight = cellHeight * countY;
    24.  
    25.         board = new GameTile[countX, countY];
    26.  
    27.         offset = new Vector2(boardWidth * 0.5f, boardHeight * 0.5f);
    28.  
    29.         for (int x = 0; x < board.GetLength(0); x++)
    30.         {
    31.             for (int y = 0; y < board.GetLength(1); y++)
    32.             {
    33.                 Vector3 position = new Vector3((x * cellWidth + cellWidth / 2) - offset.x,
    34.  (y * cellHeight + cellHeight / 2) - offset.y);
    35.                 board[x, y] = new board();
    36.             }
    37.         }
    38.     }
    39.  
    and its created a board game with sprites what i want at center relative from screen
    and i try to callculate position from mouse to my grid but every time fail because i am not cool math and suppouse callculate something wrong
    script callculate grid
    Code (CSharp):
    1. public GameTile GetTileFromMap(Vector3 worldPos)
    2.     {
    3.         float percentegeX = (worldPos.x + boardWidth / 2) / boardWidth;
    4.         float percentegeY = (worldPos.y + boardHeight / 2) / boardHeight;
    5.         percentegeX = Mathf.Clamp01(percentegeX);
    6.         percentegeY = Mathf.Clamp01(percentegeY);
    7.         float countValueX = countX - 1;
    8.         float countValueY = countY - 1;
    9.         int x = Mathf.RoundToInt(countValueX * percentegeX);
    10.         int y = Mathf.FloorToInt(countValueY * percentegeY);
    11.         return board[x, y];
    12.     }
    and it almost works as it should, just super inaccurate. for example, if you press on a cell from 0-7, then he will sometimes select it and sometimes not. apparently the surroundings are super inaccurate
     
    Last edited: Sep 6, 2022
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    You may want to check out Unity's Grid class to handle the calculations if math is not your strong suit. (math is not my strong suit, so I can't debug your math)

    https://docs.unity3d.com/ScriptReference/Grid.html

    You can convert coordinates using CellToWorld and WorldToCell, and then use the Cell coordinates to look up a tile from your board array.