como crear un cubo que encierre a un objeto en unity, script

Generate a Cube that wraps a 3D model in Unity


The objective of this solution is to find a box with the minimum volume that encloses a 3D model in Unity.

The following video is not yet dubbed, you can activate the English subtitles. I explain how to use the solution and what things to take into account when creating the 3D model in order not to have problems in Unity.

Download Files

In the download come two files, a Script called “WrappingCube” that must be assigned to the object we want to wrap with the box. The other file is a predefined transparent material, it can be assigned to the space in the inspector of the WrappingCube component, this material will be assigned automatically to the cube.

Generar Cubo que Envuelve Objeto en Unity
Version: 1.0
Published: March 31, 2020

How to use the solution

Just add this script to the object you want to wrap with a box, make sure to add it to the one that has the Mesh Renderer Component, if you add this script to an empty GameObject it won’t work.

How the wrapping box is generated

To generate the cube something called “Bounding Box” is used, which is precisely a box that encloses the object, however one detail must be taken into account and that is that this bounding box is axis-oriented, therefore if we rotate the piece, the bounding box will adjust its dimensions of width, length and height until it completely covers the model.

Therefore, for this solution to work, the 3D model must come with the standardized orientation from the 3D modelling software, this means orienting the model in a consistent way according to the global coordinate axes and then applying the rotation to the object.