Search Unity

[Solved] OverlapBox() dimensions are ginormous but are fine when isolated from script

Discussion in 'Editor & General Support' started by DevDatanaut, Aug 11, 2019.

  1. DevDatanaut

    DevDatanaut

    Joined:
    Jun 30, 2018
    Posts:
    2
    Hey all, first time poster, so bear with me.

    I'm creating this system where I'm placing level chunks together and I need to collect a list of colliders that have intersected with a particular level chunk. All of this is being done within one frame (A loop is controlling this placement). A chunk is composed of several colliders.

    The way I am doing this is with an OverlapBox, copying the dimensions of a collider of a particular child GameObject (named area). This is done through the following function, being called from a control script. It's meant to check for collisions that are happening within it, and return true when it's intersecting something it shouldn't.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CollisionCheck : MonoBehaviour
    6. {
    7.     public LayerMask m_LayerMask; //This is set to Everything in the editor
    8.  
    9.     public Collider[] collidersTouched;
    10.  
    11.  
    12.     public bool IsInappropriateTouchingHappening (GameObject allowedChunk)
    13.     {
    14.         BoxCollider area = transform.Find("area").gameObject.GetComponent<BoxCollider>();
    15.         collidersTouched = Physics.OverlapBox(area.bounds.center, area.bounds.size/2, Quaternion.identity, m_LayerMask);
    16.  
    17.         Debug.Log("OverlapBox picked up this many colliders: " + collidersTouched.Length);
    18.  
    19.         foreach (Collider col in collidersTouched)
    20.         {
    21.             Debug.Log("the collider in the obtained array: " + col.gameObject.name + col.gameObject.transform.position);
    22.             if (!GameObject.ReferenceEquals( allowedChunk, col.gameObject) && !GameObject.ReferenceEquals( gameObject, col.gameObject))
    23.             {
    24.                 return true;
    25.             }
    26.         }
    27.         return false;
    28.     }
    29.  
    30.     void OnDrawGizmos ()
    31.     {
    32.         BoxCollider area = transform.Find("area").gameObject.GetComponent<BoxCollider>();
    33.         Gizmos.matrix = Matrix4x4.TRS(area.bounds.center, Quaternion.identity, area.bounds.size);
    34.         Gizmos.color = Color.red;
    35.         Gizmos.DrawCube(Vector3.zero, Vector3.one);
    36.     }
    37. }
    It tells me that it's picked up pretty much every collider in the scene! However, when I perform an isolated test using one of the chunks that the system is using, everything checks out. Here's the code for the script I'm using for said test. It's a modified version of what's in Unity's documentation for OverlapBox.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class OverlapBoxExample : MonoBehaviour
    4. {
    5.     bool m_Started;
    6.     public LayerMask m_LayerMask;
    7.  
    8.     void Start()
    9.     {
    10.         m_Started = true;
    11.     }
    12.  
    13.     void FixedUpdate()
    14.     {
    15.         MyCollisions();
    16.     }
    17.  
    18.     void MyCollisions()
    19.     {
    20.         BoxCollider area = transform.Find("area").gameObject.GetComponent<BoxCollider>();
    21.         Collider[] hitColliders = Physics.OverlapBox(area.bounds.center, area.bounds.size/2, Quaternion.identity, m_LayerMask);
    22.         Debug.Log("Number of colliders hit in test: " + hitColliders.Length);
    23.     }
    24.  
    25.     //Draw the Box Overlap as a gizmo to show where it currently is testing. Click the Gizmos button to see this
    26.     void OnDrawGizmos()
    27.     {
    28.         BoxCollider area = transform.Find("area").gameObject.GetComponent<BoxCollider>();
    29.  
    30.         Gizmos.matrix = Matrix4x4.TRS(area.bounds.center, Quaternion.identity, area.bounds.size);
    31.         Gizmos.color = Color.yellow;
    32.         Gizmos.DrawCube(Vector3.zero, Vector3.one);
    33.     }
    34. }
    The gizmo in this second script is showing me the volume of the overlapbox correctly, but in the first script, it's not.

    Does anyone have any ideas? I cannot imagine what makes the two boxes different, aside from the OverlapBoxExample script performing its OverlapBox() inside FixedUpdate instead of frame 1 like it does in my implementation.

    Let me know if you need more information! Thanks in advance!

    EDIT: After re-reading I wanted to clear up some things, and make my issue somewhat clearer. The whole issue is that the overlapbox is way too large. Also, Both the test and the implementations' gizmos show the volume/position of the overlapbox I want. Let me know if I should post pictures.
     
    Last edited: Aug 11, 2019
  2. DevDatanaut

    DevDatanaut

    Joined:
    Jun 30, 2018
    Posts:
    2
    This is now fixed. For anyone else struggling with this, it's because the physics engine doesn't update its understanding of the locations of GameObjects and their transforms since everything is being done in one frame. Before my first call to the CollisionCheck() method, I called Physics.SyncTransforms().

    A user on SE pointed me in the right direction here: https://gamedev.stackexchange.com/q...n-isolation?noredirect=1#comment310805_174589