检查项目是否重叠
Check if items are overlapping
我有几个随机放置的房间,所以我必须检查房间是否重叠。这些房间的大小为 10x10,并且出于测试原因并排放置(它们在场景中不重叠)。 Floor 是一个由 1 个或多个变换组成的变换(在本例中是一个正方形,但对于其他形式,它可能是 2 个或更多)。
要检查它们是否重叠,我有这个不起作用的功能。调试日志总是介于 3 和 61 之间..
public bool Overlapping()
{
//Lists for the position and of the size of each floor transform
List<Vector3> positions = new List<Vector3>();
List<Vector3> sizes = new List<Vector3>();
//Check if floor consists out of more than 1 transform
if (Floor.childCount > 0)
foreach (Transform t in Floor)
{
positions.Add(t.position);
sizes.Add(t.lossyScale);
}
else
{
positions.Add(Floor.position);
sizes.Add(Floor.lossyScale);
}
//Save old room pos and move it out of the way
Vector3 position = this.transform.position;
this.transform.position = new Vector3(0, 100, 0);
//Check if any floor transform would overlap
for (int i = 0; i < positions.Count; i++)
{
//Make overlap box visible
GameObject rec = GameObject.CreatePrimitive(PrimitiveType.Cube);
rec.transform.localScale = sizes[i];
rec.transform.localPosition = positions[i];
rec.transform.localRotation = Quaternion.Euler(0, 0, 0);
//Returns the colliders which are overlapping
if (Physics.OverlapBox(positions[i], sizes[i] / 2).Length > 0)
{
Debug.Log(Physics.OverlapBox(positions[i], sizes[i] / 2).Length);
//return this room to it's old position
this.transform.position = position;
return true;
}
}
//return this room to it's old position
this.transform.position = position;
return false;
}
顺便提一下,任何阅读者 (2/2016) OverlapBox
是 Unity 刚刚添加到最新版本的全新调用。
编辑:按照 Joe 的建议,我制作了 OverlapBox 'visible',但它们的位置和大小似乎都正确(红色是我的房间, 灰色是对撞机).....
我现在开始工作了。每个 OverlapBox 都已正确放置,但它们仍然会发生碰撞,因为它们 'too close' 到对象。此更改修复了它:
if (Physics.OverlapBox(positions[i], new Vector3(sizes[i].x - 0.01f, sizes[i].y - 0.01f, sizes[i].z - 0.01f) / 2, rotations[i]).Length > 0)
我有几个随机放置的房间,所以我必须检查房间是否重叠。这些房间的大小为 10x10,并且出于测试原因并排放置(它们在场景中不重叠)。 Floor 是一个由 1 个或多个变换组成的变换(在本例中是一个正方形,但对于其他形式,它可能是 2 个或更多)。
要检查它们是否重叠,我有这个不起作用的功能。调试日志总是介于 3 和 61 之间..
public bool Overlapping()
{
//Lists for the position and of the size of each floor transform
List<Vector3> positions = new List<Vector3>();
List<Vector3> sizes = new List<Vector3>();
//Check if floor consists out of more than 1 transform
if (Floor.childCount > 0)
foreach (Transform t in Floor)
{
positions.Add(t.position);
sizes.Add(t.lossyScale);
}
else
{
positions.Add(Floor.position);
sizes.Add(Floor.lossyScale);
}
//Save old room pos and move it out of the way
Vector3 position = this.transform.position;
this.transform.position = new Vector3(0, 100, 0);
//Check if any floor transform would overlap
for (int i = 0; i < positions.Count; i++)
{
//Make overlap box visible
GameObject rec = GameObject.CreatePrimitive(PrimitiveType.Cube);
rec.transform.localScale = sizes[i];
rec.transform.localPosition = positions[i];
rec.transform.localRotation = Quaternion.Euler(0, 0, 0);
//Returns the colliders which are overlapping
if (Physics.OverlapBox(positions[i], sizes[i] / 2).Length > 0)
{
Debug.Log(Physics.OverlapBox(positions[i], sizes[i] / 2).Length);
//return this room to it's old position
this.transform.position = position;
return true;
}
}
//return this room to it's old position
this.transform.position = position;
return false;
}
顺便提一下,任何阅读者 (2/2016) OverlapBox
是 Unity 刚刚添加到最新版本的全新调用。
编辑:按照 Joe 的建议,我制作了 OverlapBox 'visible',但它们的位置和大小似乎都正确(红色是我的房间, 灰色是对撞机).....
我现在开始工作了。每个 OverlapBox 都已正确放置,但它们仍然会发生碰撞,因为它们 'too close' 到对象。此更改修复了它:
if (Physics.OverlapBox(positions[i], new Vector3(sizes[i].x - 0.01f, sizes[i].y - 0.01f, sizes[i].z - 0.01f) / 2, rotations[i]).Length > 0)