Physics.OverlapBox being too big

741 views Asked by At

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.

Picture of a 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:

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: Problem

I have tried to change transform.localScale / 2 to transform.lossy Scale / 2 but it does'nt work. What should I do?

2

There are 2 answers

0
4RZG4 On BEST ANSWER

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.

1
Thomas Finch On

Both kinds of transform scale (lossy and local) are multipliers not units, while OverlapBox is using units for it's position and size. 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. This will give you the accurate size in units.