0
\$\begingroup\$

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<Piece>();
return cp
}

  1. 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

  1. only yoffset value is helpful to place them on top of the board
  2. but different prefab pieces require different yoffset value. cause after instantiating they are not on the same base
  3. board scale is set to 1,1,1 so that parenting doesn't scale the pieces along with it
  4. 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

enter image description here

More details

original prefabs transform

enter image description here

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

enter image description here

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

enter image description here enter image description here

enter image description here

\$\endgroup\$
3
  • \$\begingroup\$ Are the objects we see in the screenshot all from the same prefab or are they different prefabs? Do those objects coming from the same prefab also have the same vertical offset? \$\endgroup\$
    – Philipp
    Jan 13 at 7:56
  • \$\begingroup\$ all are different prefabs , made from different fbx files \$\endgroup\$ Jan 13 at 8:20
  • \$\begingroup\$ updated question with more details \$\endgroup\$ Jan 13 at 8:27

2 Answers 2

0
\$\begingroup\$

It's possible that the models were not properly aligned in the 3d modeling program used to create them.

The ideal solution would of course be to tell the art department to get back to their 3d modeling program and move the meshes within their model files so that the floor is on height 0. This is usually a standard convention for game-ready 3d assets which are designed to be placed on a horizontal surface.

But when you got the assets from a source you can't make demands to, then you can fix this in Unity.

Create each piece prefab as an empty game-object with the 3d model as a child. You can now edit the prefab and adjust the local position of the child transform relative to the parent transform so that the base is exactly on the 0:0:0 point of the parent.

\$\endgroup\$
3
  • \$\begingroup\$ How it can be achieved! I tried doing changing the location to 0,0,0 and then exported it. but no success. \$\endgroup\$ Jan 13 at 15:22
  • \$\begingroup\$ @pavanjoshi914 I don't even know which 3d modeling program you are using, so I can't help you with that. But did you try the second thing I mentioned? Parenting the model in an empty game object, moving the model up within that object and then turning the whole thing into a prefab? \$\endgroup\$
    – Philipp
    Jan 13 at 15:53
  • \$\begingroup\$ Actually, I made a pivot point to the centre but vertically it was at the centre mid and not at the base. correcting this made it work. Your answer was really helpful for figuring this out , thanks a lot! \$\endgroup\$ Jan 13 at 16:59
0
\$\begingroup\$

Your GetTileCenter() seems to be taking the mid points (horizontal & vertical), but from the image, it looks like each of the prefab have different height, so even though their midpoint is alligned, because of height, it looks uneven.

For height, you can rather use the top position, new Vector3(tileSize/2,0,0);

\$\endgroup\$
2
  • \$\begingroup\$ Not working. in unity, y-direction is upward, so I took tilesize/2 only for x and z in the same plane. which is giving me the exact tile centre. using tilesize/2,0,0 neither fixing the problem , also moving chess pieces away from the center of the tile \$\endgroup\$ Jan 13 at 8:52
  • \$\begingroup\$ Sorry my bad, seemsl like your issue is with (float)(yOffSet +0.85), since the pieces are not of same height, you can get the height of mesh and calculate the top/bottom to align or do as @Phillip to parent the prefabs to empty GameObject \$\endgroup\$
    – saurabhs
    Jan 13 at 11:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .