简体   繁体   English

Physics.OverlapBox 太大

[英]Physics.OverlapBox being too big

I am doing a rubiks cube simulator.我正在做一个魔方方块模拟器。 To make the sides turn, I have a collider on each of the sides, and I make the colliders parent everything inside them on click and then just turn the side.为了让侧面转动,我在每一面都有一个碰撞器,我让碰撞器在点击时成为它们内部的所有东西的父级,然后转动侧面。

一侧的图片

To get every object inside the colliders, I use Physics.OverlapBox , and put every object except the other sides inside a list like this:为了让碰撞器内的每个对象,我使用Physics.OverlapBox ,并把每个对象除了其他边这样的列表中:

public List<GameObject> children = new List<GameObject>();

    private void Awake()
    {
        UpdateCubes();
    }

    void UpdateCubes()
    {
        Mesh mesh = GetComponent<MeshFilter>().mesh;

        children.Clear();

        foreach (Collider child in Physics.OverlapBox(transform.localPosition, 
            Vector3.Scale(mesh.bounds.size, transform.lossyScale) / 2, transform.rotation))
        {
            if (!child.transform.CompareTag("Side"))
            {
                children.Add(child.gameObject);
            }
        }
    }

Here is the problem: It seems like Physics.OverlapBox is way too big, because it gets every piece of the cube and some weird missing gameobjects, as seen here:问题在于: Physics.OverlapBox似乎太大了,因为它获取了立方体的每一部分和一些奇怪的丢失的游戏对象,如下所示: 问题

I have tried to change transform.localScale / 2 to transform.lossy Scale / 2 but it does'nt work.我试图将transform.localScale / 2更改为transform.lossy Scale / 2但它不起作用。 What should I do?我该怎么办?

Both kinds of transform scale (lossy and local) are multipliers not units, while OverlapBox is using units for it's position and size.两种变换比例(有损和局部)都是乘数而不是单位,而 OverlapBox 使用单位来表示位置和大小。 If your scale is 1 and the mesh size is 0.1, then the object takes up 0.1 units, not 1. To be sure you're accurate, you want to use Mesh.bounds.size and multiply that by the transform.lossyScale.如果您的比例为 1 且网格大小为 0.1,则对象占用 0.1 个单位,而不是 1。为确保准确,您需要使用 Mesh.bounds.size 并将其乘以 transform.lossyScale。 This will give you the accurate size in units.这将为您提供准确的单位尺寸。

I got it working!我让它工作了! The pieces from other sides were overlapping with other sides because they were so close together.其他边的碎片与其他边重叠,因为它们靠得很近。 I fixed it by putting more offset between them.我通过在它们之间放置更多偏移来修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM