【发布时间】:2014-11-02 08:23:01
【问题描述】:
我有一个随机生成的工作脚本:
PosX、PosY、ScaleX 和 ScaleY
我将随机生成的预制件放入并行数组中。从中生成随机调整大小和定位的立方体。但是,由于它是随机生成的,我如何检测代码是否存在重叠?当有重叠时,我该如何删除它?
我需要帮助编写代码。我尝试过使用光线投射和 OnTriggerEnter,但我对如何做到这一点感到困惑。我也见过使用
Physics.CheckCapsule
Physics.CapsuleCastAll
Physics.CheckSphere
Bounds.Intersect
但我不知道该怎么做。
我不知道如何在数组上使用 Physics.OverlapSphere。 我该怎么办。
void Detect_Collision_Two(int i) {
bool isOverLapped = false;
Collider[] platformsInRange = Physics.OverlapSphere(platformPrefabPosition[i], scaleX[i]/2);
foreach(Collider col in platformsInRange) {
if(col.gameObject == platformPrefab) {
continue;
}
if
}
}
编辑
尝试 这些是我尝试使用数学方法进行的尝试。它检查中心位置并考虑平台的宽度和高度。然后使用该信息与其他平台进行比较。
第二种方法是使用 OverlapSphere ,但我不知道如何实现。 请帮忙
void Platform_Position_Scale_Generator(int i) {
posX[i] = Random.Range(minPosRange, maxPosRange + 1);
posY[i] = Random.Range(minPosRange, maxPosRange + 1);
posZ[i] = 0;
scaleX[i] = Random.Range(minScaleRange, maxScaleRange + 1);
scaleY[i] = 1;
scaleZ[i] = 1;
}
void Platform_Generator(int i) {
platformPrefabPosition[i].x = posX[i];
platformPrefabPosition[i].y = posY[i];
platformPrefabPosition[i].z = posZ[i];
Instantiate(platformPrefab, platformPrefabPosition[i], Quaternion.identity);
platformPrefab.transform.localScale = new Vector3(scaleX[i], 1, 1);
}
// Error with this
void Detect_Collision(int i) {
for(int f = 0; f < i; f++) {
for(int s = f + 1; s < i; s++) {
bool xOverlap = (posX[s] > posX[f] && posX[s] < posX[f] + scaleX[i]) || (posX[f] > posX[s] && posX[f] < posX[s] + scaleX[i]);
bool yOverlap = (posY[s] > posY[f] && posY[s] < posY[f] + scaleY[i]) || (posY[f] > posY[s] && posY[f] < posY[s] + scaleY[i]);
if(xOverlap && yOverlap) {
Debug.Log("xOverlap: " + xOverlap + " yOverlap: " + yOverlap);
}
else {
//Debug.Log("xOverlap: " + xOverlap + " yOverlap: " + yOverlap);
}
}
}
}
void Detect_Collision_Two(int i) {
bool isOverLapped = false;
Collider[] platformsInRange = Physics.OverlapSphere(platformPrefabPosition[i], scaleX[i]/2);
foreach(Collider col in platformsInRange) {
if(col.gameObject == platformPrefab) {
continue;
}
if
}
}
【问题讨论】:
-
这个问题怎么太宽泛了?
-
因为它不是特定的编程问题,而是要完成的任务。 Stack Overflow 不是编码服务。请发送 tour。 首先尝试问题,然后发布具体问题 - 任何需要回答者从头开始编写代码或写一本书来解释某事的问题都过于宽泛。如果您可以将其改写为适合guidelines for reopening your question,请这样做。
-
以上是不是不够清楚?
-
我怎么能问它不是那么广泛?
-
先尝试问题,然后发布具体问题。
标签: c# arrays unity3d instantiation overlapping