如何在Unity中使用mesh.bounds? [英] How to use mesh.bounds in Unity?
问题描述
我最近创建了一个简单的关卡生成器,该生成器创建了带有分段的关卡.我使用mesh.bounds.x
来查看下一段应该放置的距离上一段.它对我来说效果很好,因为所有部分都是方形的.现在,我决定使用更大的线段和其他比例(仍然相当矩形)来进行这项工作.我的问题是,即使我尝试了多种组合,也无法正确设置.
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.
有人可以向我解释此mesh.bound
的工作原理吗?我还实例化了旋转的片段,也许这会导致一些错误?我所有的预制件都是从搅拌机进口的.
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";
}
推荐答案
您的特定问题是,边界框会在世界空间中形成一个正方形框...就是说轴对齐".
Your specific problem is that a bounding box makes a square box in world space ... that is to say "axis-aligned".
AABB代表 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.
这是3D工程的基础知识.
This is a basic in 3D engineering.
这些图显示了2D中的一些"AABB"与轴对齐"的边界框.同样适用于3D.
The diagrams show some "AABB" "axis-aligned" bounding boxes in 2D. Same applies in 3D.
(一旦您以高级方式处理此问题,也要小心,因为 mesh的边界在Unity局部空间中(即在父母的空间中)是令人困惑的AABB边界-在场景空间中使用renderer.bounds-不用担心,直到您开始使用它:)在Unity中可以使用不同的边界",它们都令人困惑.)
(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.)
顺便说一句-如果您刚开始,可以用一种KISS的方式来处理:简单地旋转对象直到它变成正方形,即不旋转. 然后进行测量!然后将其旋转到需要的位置.
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.
这篇关于如何在Unity中使用mesh.bounds?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!