0

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?

4RZG4
  • 50
  • 1
  • 10
  • well you never clear the list .. you should do `children.Clear();` before the loop .. well and then the extensions of the box completely are up to you .... you could e.g. do something like `Vector3.Scale(transform.lossyScale / 2f, new Vector3(1, 1, 0.1f))` in order to make the box smaller on the Z axis ... – derHugo Oct 06 '20 at 13:39
  • @derHugo I updated my code for better context. Also I know I could do that but what I'm asking is why does it behave like it does? – 4RZG4 Oct 06 '20 at 13:50
  • 1
    Can you check the value of the `OverlapBox` size ? Make a variable before with your computation and make a `Debug.Log` or use a breakpoint. Maybe the scale of a parent influence the final size. I suspect the size of your collider is not matching the size of the transform. – D.B Oct 06 '20 at 14:56
  • @D.B The sizes of the overlapboxes are (0.5, 0.5, 0.5) or (0.2, 0.5, 0.5) or (0.5, 0.2, 0.5) or (0.5, 0.5, 0.2) – 4RZG4 Oct 06 '20 at 16:30
  • 1
    Maybe try another approach with no collision detection process. Make a list with all your small cube and just get them by coordinate. `smallCubes.Where(cube => cube.transform.position.x == myFaceXPosition).ToList()`. You wont have scalling and selection trouble if you can know exactly the coordinate of the face you want to select. – D.B Oct 07 '20 at 09:46

2 Answers2

0

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.

Thomas Finch
  • 482
  • 2
  • 6
0

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.

4RZG4
  • 50
  • 1
  • 10