- Home /
Why isn't Bounds.Encapsulate working as expected?
Based on reading https://docs.unity3d.com/ScriptReference/Bounds.html my expectation is that if I initialize a Bounds with one object's position, then encapsulate a number of additional objects' positions, I'll end up with a bounding box that completely encloses all of the specified objects. To test this, I wrote a quick & dirty script:
public List<GameObject> objectsToEncapsulate = new List<GameObject>();
private void OnDrawGizmos()
{
Gizmos.color = Color.green;
if (objectsToEncapsulate.Count > 0)
{
Bounds newBounds = new Bounds(objectsToEncapsulate[0].transform.position, new Vector3(0,1,0));
for (int i = 0; i < objectsToEncapsulate.Count; i++)
{
newBounds.Encapsulate(objectsToEncapsulate[i].transform.position);
}
Gizmos.DrawWireCube(newBounds.center, newBounds.extents);
}
}
This seems to produce weird results when I run it:
The above is a screenshot of the test in action, the only objects in objectsToEncapsulate are the four cubes but instead of the wireframe outline completely encapsulating them, it seems to just be transcribing a smaller internal section of their area- am I just completely misunderstanding how bounds are supposed to behave?
Answer by Dragate · Jul 15, 2019 at 02:05 PM
Extents is always half of the size. As DrawWireCube() requests the size as the 2nd argument, you should have given the size (not the extents) of the bounds. If you want to fully encapsulate your objects (not only their centers), it's best to give Encapsulate() the bounds of the objects instead of their position.
Oh that makes perfect sense, thank you for clarifying!
Your answer
Follow this Question
Related Questions
Skinned Mesh disappears if the camera is too close. 0 Answers
Reporting the vertices of a 3D Bounding Box to Screen position 0 Answers
"Recalculate" the mesh bounds to the objects rotation? 3 Answers
Is the gameobject is on the same level with it's surface on which it is located? 0 Answers
render the bounding box of collider or mesh how to? 2 Answers