I have generated a logical grid and trying to place a prefab on top of that grid.
Piece
is the base class for all pieces
Pieces[]
is the 2d array holding all the pieces
1). instantiate and spawn prefab
private Piece spwanSinglePiece()
{
Piece cp = Instantiate(prefabs[0], transform, false).GetComponent();
return cp
}
2). Position them
private void PositionSinglePiece(int x, int y, bool force = false)
{
Pieces[x, y].transform.position = GetTileCenter(x, y);
}
private Vector3 GetTileCenter(int x, int y)
{
return new Vector3(x * tileSize, (float)(yOffSet +0.85), y * tileSize) - bounds + new Vector3(tileSize/2,0,tileSize/2);
}
Scenario
- only yoffset value is helpful to place them on top of the board
- but different prefab pieces require different yoffset value. cause after instantiating they are not on the same base
- board scale is set to 1,1,1 so that parenting doesn’t scale the pieces along with it
- created prefabs scale is also 1.
Problem
prefabs are not exactly on the same horizontal alignment. which is resulting in the use of different yoffset values for each piece.
if all the pieces are aligned horizontally same they single yoffset value can work this what I am trying to achieve