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.
- Unity Physics
- Unity Collision Detection
- Character Controller and Input Systems
- Rigidbody Mass in Unity
- How to add Friction to a Rigidbody
- Cylinder Collider in Unity
- Box Collider in Unity
- Box Cast in Unity
- Sorting Layers
- Get the distance between two objects
- 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
- 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.
- Is Trigger: This property allows other colliders to pass through this collider when enabled and serves as a trigger for events.
- Provides Contacts: This property enables the collider to generate contact data. It is disabled by default.
- Material: This property adds the Physic Material asset to the collider which in turn determines the friction and bounciness of the collider.
- Layer Overrides: This section has three properties.
- Layer Override Priority: Defines the priority of override when two colliders have conflicting overrides. The collider with the higher value is prioritized.
- Include Layers: Chooses the layers of the collider to include in collisions.
- Exclude Layers: Chooses the layers of the collider to exclude in collisions.
- 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?
- Click the Add Component button in the Inspector window.
- 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:
- OnCollisionEnter: called when a collision is initiated.
- OnCollisionExit: called when a collision has stopped.
- OnCollisionStay: called during collision.
- OnTriggerEnter: called when there’s a collision between two game objects.
- OnTriggerExit: called when collision has stopped between the collider and trigger.
- 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!