检查项目是否重叠 [英] Check if items are overlapping

查看:85
本文介绍了检查项目是否重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个随机放置的房间,所以我必须检查房间是否重叠.房间的大小为 10x10,出于测试原因,它们完全并排放置(它们在场景中不重叠).地板是一种变换,它由 1 个或多个变换组成(在这种情况下是一个正方形,但对于其他形式,它可能是 2 个或更多).

I have a few rooms which are placed randomly so I have to check if a room is overlapping. The rooms have a size of 10x10 and are for test reasons placed exactly side by side (they don't overlap in the scene). The Floor is a Transform which consists out of 1 or more Transforms (in this case out of one square but for other forms it it could be 2 or more).

为了检查它们是否重叠,我有这个不起作用的功能.调试日志总是在 3 到 61 之间..

To check if they are overlapping I have this function which doesn't work. Debug log is always something between 3 and 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 刚刚添加到最新版本的全新调用.

By the way for anyone reading (2/2016) OverlapBox is a brand-new call Unity just added to the latest verion.

编辑:按照 Joe 的建议,我将 OverlapBox 设为可见",但它们似乎处于正确的位置和正确的尺寸(红色是我的房间,灰色是对撞机)....

Edit: As suggested by Joe I made the OverlapBox 'visible', but they seem to be in the correct positions and in the correct sizes (Red is my room, gray are the colliders)....

推荐答案

我现在开始工作了.每个 OverlapBox 都被正确放置,但它们仍然碰撞,因为它们太靠近"对象.此更改修复了它:

I got it working now. Each OverlapBox was placed correctly, but they where still colliding, because they were 'too close' to the object. This change fixed it:

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)

这篇关于检查项目是否重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆