Everything You Need to Know about Box Collider in Unity

by Michael Sacco Published January 14, 2024
The cover for Everything You Need to Know about Box Collider in Unity

Colliders are indispensable components in Unity as they contribute to the movement characteristics of your game objects. There are different collider types based on their shape configurations. Box Collider is one of the simple and common types of colliders in Unity.

In this article, you will learn everything you need to know about Box Collider in Unity.

This is a series on Unity Physics. We recommend reading the series in order.

  1. Unity Physics
  2. Unity Collision Detection
  3. Character Controller and Input Systems
  4. Rigidbody Mass in Unity
  5. How to add Friction to a Rigidbody
  6. Cylinder Collider in Unity
  7. Box Collider in Unity
  8. Box Cast in Unity
  9. Sorting Layers
  10. Get the distance between two objects
  11. How to normalize a vector

What is a Box Collider?

One of the most common types of colliders in Unity is the Box Collider. It has the shape of a cube and is useful for anything box-shaped like a crate or chest. It can also be stretched or flattened to make floors or walls. It is one of the primitive collider shapes.

Properties of a Box Collider

  1. Edit Collider: This property displays the collider’s contact point when enabled. These contact points can be used to modify the shape and size of the collider.
  2. Is Trigger: This property allows other colliders to pass through this collider when enabled and serves as a trigger for events.
  3. Provides Contacts: This property enables the collider to generate contact data. It is disabled by default.
  4. Material: This property adds the Physic Material asset to the collider which in turn determines the friction and bounciness of the collider.
  5. Layer Overrides: This section has three properties.
  6. Layer Override Priority: Defines the priority of override when two colliders have conflicting overrides. The collider with the higher value is prioritized.
  7. Include Layers: Chooses the layers of the collider to include in collisions.
  8. Exclude Layers: Chooses the layers of the collider to exclude in collisions.
  9. Center and Size: These are properties that determine the position, size, and shape of the collider.

How to Add a Box Collider to Your Game Object?

  1. Click the Add Component button in the Inspector window.
  2. Select Box Collider from the dropdown menu.

By doing this, you have successfully added a Box Collider to your Game Object. Ensure you modify the properties according to your specifications.

How to Enable a Box Collider Component?

Ensure you have added the Box Collider component to your Game Object following the steps earlier.

Declare the Game Object you added the Box Collider to in your script.

public GameObject MyGameObject;

Using the GetComponent method, set the enabled event of the Box Collider to true to enable.

MyGameObject.GetComponent<BoxCollider>().enabled = true;

How to Disable a Box Collider Component?

Ensure you have added the Box Collider component to your Game Object following the steps earlier.

Declare the Game Object you added the Box Collider to in your script.

public GameObject MyGameObject;

Using the GetComponent method, set the enabled event of the Box Collider to false to disable.

MyGameObject.GetComponent<BoxCollider>().enabled = false;

Box Collider Messages

A Box Collider component has built-in messages that are called when an event is triggered. They are:

  1. OnCollisionEnter: called when a collision is initiated.
  2. OnCollisionExit: called when a collision has stopped.
  3. OnCollisionStay: called during collision.
  4. OnTriggerEnter: called when there’s a collision between two game objects.
  5. OnTriggerExit: called when collision has stopped between the collider and trigger.
  6. OnTriggerStay: called during a collision between the collider and trigger.

Now, you have learned the basic things you need to know about a Box Collider component. Don’t forget to try out all you have learned in this article. As usual, don’t forget to visit Unity’s official docs to learn more about colliders in general.

Happy creating!

Free download: Indie Game Marketing Checklist

Download now

Category

physics

Don't forget to share this post!

Popular assets for Unity

See all assets ->
    Cutting-edge volumetric fog and volumetric lighting with support for transparent materials.
    Volumetric clouds, day night cycles, dynamic skies, global lighting, weather effects, and planets and moons.
    A lightweight procedural skybox ideal for semi-stylized projects.
    Image-based Outlines for 2D and 3D games with variable line weight, color, and displacement options.
    Drag-and-drop ready-to-use ambient, impact, and spell particle effects.
    Per-pixel gaussian blur on your entire screen, part of the UI, or in-scene objects.

Free Indie Game Marketing Checklist

Learn how to make your game successful with this handy checklist.

Download for free