Unity bounds

Unity Bounds – What is a bbox?

On the Unity platform, the Bounds structure is the C# representation of a bounding box.

This box, frequently used in mapping, is characterized by an area defined by its size and center.
In the Unity ecosystem, it is notably applied to represent the volumes of a collider.

Table of Contents

What is Bounds?

Bounds, or “An axis-aligned bounding box” (AABB), is an axis-aligned box, also known by the acronym bbox in the mapping domain.

In Unity, this structure is defined by a center and a size, and it does not have rotation.

Unity bounds displaySphere with a bounding box in red

Renderers, Colliders, and Mesh use Bounds to represent their volume.

Note: With a collider, you cannot directly modify the volume using the bounds property. Specific collider properties such as size should be used for modifications.

Why use Bounds in Unity?

Bounds, as a box, allows for various calculations with better performance than a collider.

The performance boost depends on the type of collider used.
For example, in Unity, using a box collider remains more performant than a polygon collider.
However, it’s essential to note that the collider is more precise, while Bounds is coarser in its calculations.

The choice between a costly but precise function and a fast but less precise function depends on the type of game you are creating and your needs.

Additionally, Bounds is easy to use, providing a set of ready-to-use functions for collisions and raycasting, as well as ease of resizing.

Unity Bounds – How to use it?

For Renderer, Collider, and Mesh components, Bounds is automatically created based on the component’s values.

It can be retrieved using the bounds property: var bounds = mesh.bounds;

Bounds can also be created via a constructor by passing a center and a size as arguments: var bounds = new Bounds(Vector3.zero, Vector3.one);

Its volume can be modified with the following properties:

  • size: the size of the bbox.
  • center: the center of the bbox.
  • min: the minimum points of the bbox.
  • max: the maximum points of the bbox.
  • extents: the size divided by 2.

Each of these properties is a Vector3.

Methods such as SetMinMax, Encapsulate, and Expand can also be used for these modifications.

Modifications made on a bounds attached to a component will not change the rendering on the screen.

Bounds is only a visual representation but is used, for example, for culling.

Collision-Specific Methods

Now let’s get to the most interesting part. Bounds has methods specific to collisions.

Avoiding delving back into our math courses…

To present the methods, let’s start with an area retrieved from a collider:

using UnityEngine;

public class BoundsBehaviour : MonoBehaviour
{
  private Collider _collider;

  private void Awake()
  {
    _collider = GetComponent<Collider>();
  }
}
  

ClosestPoint

We define a point in the scene, and the ClosestPoint method returns the closest point in our area.

var closestPoint = _collider.bounds.ClosestPoint(positionEnemy);
  

Unity ClosestPointIn blue, the point passed as a parameter; in yellow, the closest point.

Contains

Allows us to know if the point passed as an argument is inside or outside of the area. Contains returns true if so.

if (_collider.bounds.Contains(positionPlayer)) {
    // if the player is in the area, do something
  }

IntersectRay

Determines if a ray intersects our area. IntersectRay returns true if so.

// create ray, like a gun fire
var ray = new Ray(playerPosition, playerDirection);

// Check if the ray hits our bbox
if (_collider.bounds.IntersectRay(ray, out float distance)) {
  // Player has hit his target
}

Unity IntersectRayIn blue, the ray piercing the bbox

Intersects

Determines if another bounds is in contact with the calling bounds. Intersects returns true if so.

// bounds are in collision
if (_collider.bounds.Intersects(otherBounds)) {

}

SqrDistance

Returns the smallest squared distance between the bounds and a point.
SqrDistance returns a float.

// Calculate the min distance between bbox and player
var minDistance = _collider.bounds.SqrDistance(playerPosition);

FAQ

What is the difference between Bounds and Rect in Unity?

The difference between Bounds and Rect is that Bounds works in a 3D world, while Rect only works in 2D. Additionally, Bounds has a greater number of methods, such as calculating the closest point, ray intersection, etc.

Leave a Reply

Your email address will not be published. Required fields are marked *