Question about bounds.extents and how it is used

Hi there

I’m trying to learn about Bounds.extents at the moment and I’m having trouble trying to understand what extents are and how they are used. I know that extents are described as “the extents of the Bounding Box which is always half the size of the bounds.” I got that description from looking at the following link: Unity - Scripting API: Bounds.extents

I don’t know what this means in layman’s terms?

In order to try to understand what extents actually are I wrote the following code in a script that is attached to a quad that has a BoxCollider2D attached to it as shown here:

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

public class Player : MonoBehaviour
{

      Collider2D _collider2D;
    public Vector2 _myScale;
    // Start is called before the first frame update
    void Start()
    {
        _collider2D = gameObject.GetComponent<Collider2D>();
        //Output the GameObject's Collider Bound extents
      
    }

    // Update is called once per frame
    void Update()
    {
        Debug.Log("extents : " + _collider2D.bounds.extents);
    }
}

So when I run the code I get the extents of the Bounding Box outputted to the console. The output seems to be presented in X,Y and Z co-ordinates. If I use the “Rect Tool” while in scene view when this code is running and drag the right side of the quad towards the right the X value of the extents changes along with the X value in Scale within the Transform component of the Quad.

So are extents similar in nature to the Scale values of the Transform component? The X and Y values of the extents of the Bounding Box are basically the lengths of units that the Bounding Box occupies in world space? Would this diagram that I’ve created be correct?: Bounds-Extents hosted at ImgBB — ImgBB

extents is a Vector that is related to the “size” of the bounding box. You can think of it as a arrow that goes from the center of the bounding box to the “maximum” corner of the box. Here’s a 2D representation:

7003400--827864--upload_2021-4-3_12-10-24.png
The x and y components of extents are half of the width and half of the height of the box, respectively (and z is half of the “depth”).

You can think of it as the “wingspan” of the bounding box, or how far the box “extends” from its center point in each dimension.

9 Likes

A use case that I’ve seen is for infinite scrolling background with “leapfrogging” sprites. By using bounds.extents you don’t have to hardcode when to leapfrog, no matter the sprite’s width.