MeshRenderer has wrong bounds when rotated

Hi. When I try to get the bounds of my models (created in Blender) and show them in Inspector:

As you can see the bounds are correct when the objects are not rotated. But when they are (left-most object) bounds start getting totally wrong.

Here is a script that shows / gets the bounds:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GetBounds : MonoBehaviour
{
    public MeshRenderer mesh_renderer = null;

    public bool show_bounds = false;

    private void OnDrawGizmos()
    {
        if (!show_bounds) return;

        Gizmos.DrawWireCube(mesh_renderer.bounds.center, mesh_renderer.bounds.size);
        Gizmos.DrawWireSphere(mesh_renderer.bounds.center, 0.3f);
    }
}

How can I fix this?

From this documentation:

https://docs.unity3d.com/ScriptReference/Mesh-bounds.html

It seems they bounds-box it in local space, then if you have rotated the transform, they recompute bounds at the new world-aligned axis around that local box, but it isn’t really specified explicitly. Anything not explicitly promised in an API is dangerous to rely upon for future functionality, so it might be you need to compute your own bounds if it is that critical to your app functionality.

You can do your own bounds computation as there are plenty of open source examples out there, and it is trivially easy to pluck vertex information out of a Unity3D MeshFilter’s mesh.

1 Like