首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >OverlapBox()尺寸很大,但隔离测试时很好

OverlapBox()尺寸很大,但隔离测试时很好
EN

Game Development用户
提问于 2019-08-13 02:26:46
回答 1查看 1.3K关注 0票数 1

为了做到完全透明,我或多或少地抄袭了联合论坛上的这个问题,因为我没有收到回复。

我正在创建这个系统,在这个系统中,我将级别块放在一起,我需要收集与特定级别块相交的对撞机列表。所有这些都是在一个帧内完成的(一个循环控制这个位置)。一块由几个对撞机组成。

我这样做的方法是使用OverlapBox,复制特定子GameObject (命名区域)对撞机的尺寸。这是通过从控制脚本调用的以下函数来完成的。它的目的是检查内部发生的碰撞,并在与不应该发生的冲突交叉时返回true。

代码语言:javascript
运行
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CollisionCheck : MonoBehaviour
{
    public LayerMask m_LayerMask; //This is set to Everything in the editor

    public Collider[] collidersTouched;


    public bool IsInappropriateTouchingHappening (GameObject allowedChunk)
    {
        BoxCollider area = transform.Find("area").gameObject.GetComponent<BoxCollider>();
        collidersTouched = Physics.OverlapBox(area.bounds.center, area.bounds.size/2, Quaternion.identity, m_LayerMask);

        Debug.Log("OverlapBox picked up this many colliders: " + collidersTouched.Length);

        foreach (Collider col in collidersTouched)
        {
            Debug.Log("the collider in the obtained array: " + col.gameObject.name + col.gameObject.transform.position);
            if (!GameObject.ReferenceEquals( allowedChunk, col.gameObject) && !GameObject.ReferenceEquals( gameObject, col.gameObject))
            {
                return true;
            }
        }
        return false;
    }

    void OnDrawGizmos ()
    {
        BoxCollider area = transform.Find("area").gameObject.GetComponent<BoxCollider>();
        Gizmos.matrix = Matrix4x4.TRS(area.bounds.center, Quaternion.identity, area.bounds.size);
        Gizmos.color = Color.red;
        Gizmos.DrawCube(Vector3.zero, Vector3.one);
    }
}

它告诉我,几乎每一个对撞机在现场都捡到了!但是,当我使用系统正在使用的一个块执行隔离测试时,所有内容都会被检查出来。下面是用于上述测试的脚本的代码。它是统一文档中OverlapBox文档的一个修改版本。

代码语言:javascript
运行
复制
using UnityEngine;

public class OverlapBoxExample : MonoBehaviour
{
    bool m_Started;
    public LayerMask m_LayerMask;

    void Start()
    {
        m_Started = true;
    }

    void FixedUpdate()
    {
        MyCollisions();
    }

    void MyCollisions()
    {
        BoxCollider area = transform.Find("area").gameObject.GetComponent<BoxCollider>();
        Collider[] hitColliders = Physics.OverlapBox(area.bounds.center, area.bounds.size/2, Quaternion.identity, m_LayerMask);
        Debug.Log("Number of colliders hit in test: " + hitColliders.Length);
    }

    //Draw the Box Overlap as a gizmo to show where it currently is testing. Click the Gizmos button to see this
    void OnDrawGizmos()
    {
        BoxCollider area = transform.Find("area").gameObject.GetComponent<BoxCollider>();

        Gizmos.matrix = Matrix4x4.TRS(area.bounds.center, Quaternion.identity, area.bounds.size);
        Gizmos.color = Color.yellow;
        Gizmos.DrawCube(Vector3.zero, Vector3.one);
    }
}

第二个脚本中的gizmo正确地显示了OverlapBox的卷,但在第一个脚本中,它不是。

有人有什么想法吗?除了OverlapBoxExample脚本在FixedUpdate中执行它的OverlapBox(),而不是框架1,就像它在我的实现中所做的那样,我无法想象这两个框的不同之处。不过,我不知道为什么会影响到这个尺寸。

综上所述,整个问题是,我试图在第一个脚本中创建的,是令人费解的大。此外,测试和实现的小发明都显示了我想要的OverlapBox的数量/位置。如果需要更多的信息,请告诉我。

EN

回答 1

Game Development用户

发布于 2019-08-13 19:04:52

多亏了DMGregory所指出的,我能够解决我的问题。在我第一次调用Physics.SyncTransforms (统一文件)之前,我不得不使用OverlapBox()

事实证明,OverlapBox并不是非常大,但是由于所有的事情都发生在一个帧中,所以需要手动更新转换。因此,OverlapBox的尺寸是正确的,但是根据物理引擎,我移动过的所有对撞机实际上还没有移动。因此,在使用OverlapBox之前,我只需要进行这个调用:

代码语言:javascript
运行
复制
Physics.SyncTransforms()
票数 2
EN
页面原文内容由Game Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://gamedev.stackexchange.com/questions/174589

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档