【问题标题】:How to detect overlap from randomly generated prefabs [closed]如何检测随机生成的预制件的重叠[关闭]
【发布时间】: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


【解决方案1】:

您需要制作一个 overlapSphere,以您的对象位置中心为中心,bounds extent为其radius,并检查所有colliders 如果他们的边界与新的对象边界相交

 bool isOverlapped = false;
 Bounds bounds = renderer.bounds;
 Collider[] cols = Physics.OverlapSphere(transform.position, bounds.extents.magnitude);
 foreach(Collider col in cols) {
     if (col.gameObject == gameObject) {
         continue; 
     }
     if (bounds.Intersects(col.gameObject.renderer.bounds)) {
         isOverlapped = true;
         break;
     }
 }

在此之后,您可以使用 isOverlapped 布尔值像这样销毁您的对象

 if(isOverlapped)
         Destroy (gameObject);

【讨论】:

  • 那么这是原始平台预制的脚本吗?因为我所做的是,我在生成平台的层次结构中制作了一个游戏对象。抱歉,我不知道如何在我的游戏中实现这一点。你能说得更详细些吗?谢谢 。 .
  • 你应该在你想要创建的每个对象上使用它,以确保它在创建时不会与任何东西发生冲突
  • 自从我将所有内容都记录在一个数组及其并行中。您需要对代码进行哪些更改以满足我的需要?
  • 当你想生成 aomething 时使用它
  • 所以。 . Sphere 的半径是多少?
猜你喜欢
  • 2018-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-10
  • 1970-01-01
  • 2012-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多