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
- instantiate and spawn prefab
private Piece spwanSinglePiece() {
Piece cp = Instantiate(prefabs[0], transform, false).GetComponent<Piece>();
return cp
}
- 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
More details
original prefabs transform
clone prefab generated after running script. every clone prefab has a y transform of 1.05 after being generated. which is expected but all prefabs are not on same horizontal line.
so they are in different vertical position. I am not sure what really causing a issue
update - using primitive objects, scripts just run fine and produce expected results.
I think its a problem with the pieces that were created in the blender and exported into Unity
1. after creating the mesh, rotating and scale applied
2. centred origin to geometry and pivot to the median point
3. normals recalculated
4. while exporting settings where as follows