【发布时间】:2016-02-07 13:27:03
【问题描述】:
我有几个房间是随机放置的,所以我必须检查一个房间是否重叠。房间的大小为 10x10,出于测试原因,它们完全并排放置(它们在场景中不重叠)。 Floor 是一个 Transform,由 1 个或多个 Transforms 组成(在这种情况下,由一个正方形组成,但对于其他形式,它可能是 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 设置为“可见”,但它们的位置和大小似乎都正确(红色是我的房间,灰色是对撞机)。 ...
【问题讨论】:
-
顺便说一句。变换只是一个“点”。你的意思是对撞机、边界或其他任何东西
-
重叠框:Documentation
-
变换不是表示游戏对象的可见部分吗?
-
一点也不,
Transform拥有任何GameObject的位置(实际上是缩放等)。请注意,通常游戏 GameObject 甚至没有可见区域,它可能只是一个组件或其他任何东西 - 与任何可见的东西无关。 -
ATransform 只是一个包含位置、旋转和缩放的类。它基本上是空间中的一个点。然后,您将拥有由许多基于该变换定位在空间中的点组成的网格。通过渲染器的网格是游戏对象的视觉部分(相当简化)。一个空的游戏对象是一个变换,没有别的。
标签: c# random unity3d overlap collider