1

I recently created simple level generator, which creates a level with segments. I used mesh.bounds.x to see how far from the previous segment that the next segment should be placed. It worked for me pretty well, because all segments were square. Now I decided to make this work with bigger segments with the other sides proportions(still quite rectangular). My problem is that even though I tried many combinations I couldn't set this up properly.

Can somebody explain to me how this mesh.bound works? I also instantiate rotated segments, maybe this causes some bugs? All my prefabs are imported from blender.

There is part of my code where I instantiate one segment(before I decided to use more complex segments):

size = meshh.bounds.size;

if ((x == 0) && (y == 0))
{
     segments[x, y] = Instantiate(prefabs[0], new Vector3(segments[1, 1].transform.position.x - size.x, segments[1, 1].transform.position.y, segments[1, 1].transform.position.z + size.x), segments[1, 1].transform.rotation) as GameObject;

     segments[x, y].tag = "Terrain";
}
James Hogle
  • 3,010
  • 3
  • 22
  • 46
Wojtek Wencel
  • 2,257
  • 6
  • 31
  • 65

2 Answers2

5

Your specific problem is that a bounding box makes a square box in world space ... that is to say "axis-aligned".

AABB stands for AXIS ALIGNED bounding box.

It IS NOT the "bounds" as you would think of "bounds" in the conventional English meaning of that word.

Exactly as you say, if you have a rotated object, it gives you the biggest "flat" box in world space which holds the thing.

This is a basic in 3D engineering.

enter image description here

The diagrams show some "AABB" "axis-aligned" bounding boxes in 2D. Same applies in 3D.

(Once you deal with this in an advanced way, be careful too because the mesh's bounds are confusingly AABB bounds in Unity-local space i.e. in the parent's space - use renderer.bounds for scene-space - don't worry about this until you get in to it :) There are different "bounds" you can use in Unity, all of them confusing.)

By the way -- one KISS way to deal with this if you're just starting out: simply, rotate the object until it is square, ie no rotation. Then get the measurement! Then rotate it to where you need to.

Fattie
  • 27,874
  • 70
  • 431
  • 719
2

Bounds describe the the smallest box your mesh would fit in. Box is in world space. Basically like these white corners in 3ds Max:

3dsMax selection box

Mesh.bounds on Unity Docs

Fattie
  • 27,874
  • 70
  • 431
  • 719
Nika Kasradze
  • 2,834
  • 3
  • 25
  • 48