Skip to main content
added 346 characters in body
Source Link
Zillo
  • 131
  • 3

You are getting the topmost vertex, and setting the coordinates to this point. The problem is, on a box you have 4 top vertices if it is placed with a 0 rotation angle. One for each corner of the face.

There are multiple ways to solve this, but using what code you already have, you can add the topvertices in a list if they are equally high and then you can take the average position of those. This will bring it into the middle. However, by doing this you will see artifacts when rotating the object, the UI will jump around as the calculations will be different.

Therefore it would be better to just take the center position of the object, take the bounding volume of the mesh, and add half of the bounding volume height to the center position. Then you would be placing your UI in the center of the object at the same height just above the mesh at all times.

EDIT: Here is some code to do this, however note that I am not at my computer to verify, but it is something along the lines of:

Mesh mesh = GetComponent<MeshFilter>().mesh;
Bounds bounds = mesh.bounds;
Float halfheight = bounds.extents.y;
Vector3 pos = new Vector3(bounds.center.x, bounds.center.y+halfheight, bounds.center.z);

You are getting the topmost vertex, and setting the coordinates to this point. The problem is, on a box you have 4 top vertices if it is placed with a 0 rotation angle. One for each corner of the face.

There are multiple ways to solve this, but using what code you already have, you can add the topvertices in a list if they are equally high and then you can take the average position of those. This will bring it into the middle. However, by doing this you will see artifacts when rotating the object, the UI will jump around as the calculations will be different.

Therefore it would be better to just take the center position of the object, take the bounding volume of the mesh, and add half of the bounding volume height to the center position. Then you would be placing your UI in the center of the object at the same height just above the mesh at all times.

You are getting the topmost vertex, and setting the coordinates to this point. The problem is, on a box you have 4 top vertices if it is placed with a 0 rotation angle. One for each corner of the face.

There are multiple ways to solve this, but using what code you already have, you can add the topvertices in a list if they are equally high and then you can take the average position of those. This will bring it into the middle. However, by doing this you will see artifacts when rotating the object, the UI will jump around as the calculations will be different.

Therefore it would be better to just take the center position of the object, take the bounding volume of the mesh, and add half of the bounding volume height to the center position. Then you would be placing your UI in the center of the object at the same height just above the mesh at all times.

EDIT: Here is some code to do this, however note that I am not at my computer to verify, but it is something along the lines of:

Mesh mesh = GetComponent<MeshFilter>().mesh;
Bounds bounds = mesh.bounds;
Float halfheight = bounds.extents.y;
Vector3 pos = new Vector3(bounds.center.x, bounds.center.y+halfheight, bounds.center.z);
Source Link
Zillo
  • 131
  • 3

You are getting the topmost vertex, and setting the coordinates to this point. The problem is, on a box you have 4 top vertices if it is placed with a 0 rotation angle. One for each corner of the face.

There are multiple ways to solve this, but using what code you already have, you can add the topvertices in a list if they are equally high and then you can take the average position of those. This will bring it into the middle. However, by doing this you will see artifacts when rotating the object, the UI will jump around as the calculations will be different.

Therefore it would be better to just take the center position of the object, take the bounding volume of the mesh, and add half of the bounding volume height to the center position. Then you would be placing your UI in the center of the object at the same height just above the mesh at all times.