UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

physics

[PhysX] BoxCollider Bounds Return Default Values After Assignment

Solution

collideroptimizationcollision

Unity 2021.x - Unity 6.3.x

Published Thu, Mar 26

Issue

 An issue occurs where the BoxCollider.bounds property fails to update after modifying size or center at runtime. Even after programmatic assignment, the BoxCollider.bounds accessor may return a default Axis-Aligned Bounding Box (AABB) centered at the origin, causing logic failures in distance checks, visibility testing, or manual collision routines despite the visual collider appearing correct in the Scene View.

The BoxCollider.bounds property is a world-space calculation that relies on the physics engine state; if the engine has not synchronized or the Physics Handler is misconfigured, it returns stale or default data.

Explanation

The bounds property is a world-space representation calculated by the physics engine. If the engine is not actively tracking the BoxCollider, it fails to compute the AABB correctly.

  1. Navigate to Edit > Project Settings > Physics.
  2. Ensure the Physics Handler is set to PhysX. If it is set to None, the physics engine will not process collider updates or simulate any interactions.
  3. If your script modifies size or center, call Physics.SyncTransforms() immediately before accessing bounds to force a refresh of the internal spatial data.
  4. Confirm your GameObject is active in the hierarchy, as disabled objects do not update their physics bounds.

Additional Tips:

  • Use localBounds in versions supporting it if you only need the collider dimensions relative to the object.
  • Remember that bounds is an encapsulation of the collider in world-space, meaning it will change as the Transform rotates or scales, unlike size.
  • If you are modifying colliders inside a loop, calling Physics.SyncTransforms() once at the end of the loop is more performant than calling it for every individual object.

Copy


using UnityEngine;

public class ColliderBoundsUtility : MonoBehaviour
{
    public BoxCollider UpdateCollider(MeshRenderer renderer)
    {
        BoxCollider bc = renderer.gameObject.GetComponent<BoxCollider>();
        if (bc == null)
        {
            bc = renderer.gameObject.AddComponent<BoxCollider>();
        }

        // Assigning values to size/center does not immediately refresh world-space bounds
        bc.center = renderer.localBounds.center;
        bc.size = renderer.localBounds.size;

        // Force physics synchronization to update the bounds property
        Physics.SyncTransforms();

        Debug.Log($"Actual World Bounds: {bc.bounds}");
        return bc;
    }
}

Related Posts Haven't quite found a solution to your problem? We think these posts might help you.

Content inspired by a Unity discussion post.