What Does a Collider Do in Unity?

In a game, players, enemies, and obstacles are bound to hit each other at some point. When that happens, a collider is necessary to handle these events.

A collider detects collisions between GameObjects. The Collision class holds specific information about the collision that can be sent to the Collider class and used in specific cases to cause a specific action to take place using functions, such as, OnCollisionEnter(), OnCollisionStay(), and OnCollisionExit().

You may have used colliders in the past, but you may only know certain things about their properties and use. Let’s find out more about colliders.

The Box Collider Component

There are several types of colliders you can use to add to a GameObject. The BoxCollider is one of them. This is a basic collider used for collision detection of cubes and rectangular prisms.

Now check out the Box Collider Component below to see what we can do to edit this collider.

Edit Collider – When you press this button, you can easily manipulate the Box Colliders proportions in the Scene View. You cannot change the shape of the Box Collider so that it is not a cube or rectangular prism anymore. You can only change the scale of the collider.

You can change the scale by clicking, holding, and dragging one of the small squares on one of sides of the Box Collider. The top and bottom squares will manipulate the Y scale, and the side squares will manipulate the X or Z scales.

Is Trigger- This setting, when enabled on a specific Collider, makes it so that other Colliders simply pass through it when they collide with it. The physics engine ignores the collision. You may wonder, then what’s the point of it? Well, it is used to start, or trigger, a specific event. Thus the word “Trigger” in the name.

In games, Trigger Colliders can be a pick-up, that when the player’s Collider collides with it, causes the player to receive a power-up. Or, many times, it is an area in the level that ends the level when the player’s Collider enters the Trigger Collider.

In the image below, there are two cube GameObjects, one is blue and one is red. Both of them have a Box Collider attached. They are both in the exact same position. As you can see, even though the two Box Colliders are colliding, nothing is happening. They are still taking up the same space.

Now, if we add a Rigidbody to the blue cube, something changes. Because one of the GameObjects has a Rigidbody attached, it is now using the physics engine. So that collider cannot take up the same space as another collider. Unity will move the GameObject with the Rigidbody out of the space taken up by the other GameObject.

What if one of the colliders is a Trigger Collider? Then the collider with the Rigidbody can take up the same space as the Trigger Collider. Remember, the Trigger Collider is ignored by the physics engine. Therefore, other Colliders can pass through it.

Material – This parameter takes a Physic Material. A Physic Material is a special material that can affect attributes of a GameObject, specifically when it comes to a GameObject’s bounciness and how friction affects it when it is sliding across another GameObject.

In the image below, the blue cube and red cube both have Rigidbodies and are dropped from 10 on the Y axis. The blue cube has a Physic Material attached to it with a Bounce value of one. The red cube has a different Physic Material so that it has zero friction. Finally, the white plane below the cubes also has zero friction and is rotated -10 degrees.

The blue cube bounces several times using realistic physics and the red cube slides down the plane. Both meet their demise in the depths below.

Center- This is the X, Y, and Z positioning of the Box Collider. When it is at 0, 0, 0, then it’s center is the same as the cube GameObject’s center.

Size- This is the size of the Box Collider. By default, it will be the size of the cube GameObject. You can make it bigger or smaller than the GameObject. When it is too different of a size than the cube GameObject, collisions will not look like they happen when the GameObject is hit, or the collisions will happen within the GameObject.

As you can see in the above image, the blue cube’s Box Collider is much larger than the cube and the collider hits the ground before the blue cube GameObject does. The Box Collider’s center is also offset from the cube GameObject’s center by 0.5 on the X axis.

The Sphere Collider

This collider, as its name suggests, is used for collision of spherical GameObjects. Below is the Sphere Collider Component.

As you can see from this image, it has mostly the same properties of the Box Collider. However, instead of a size property, it is a radius.

Radius- The radius of a circle is the straight distance from its center to a location along the circumference of the circle. The radius property dictates the total size of the Sphere Collider.

When you add a Sphere GameObject to the scene, it defaults to the scale of 1, 1, 1. Adding a Sphere Collider to that GameObject will default it’s radius’ size to 0.5. This perfectly covers the sphere. So to perfectly cover a Sphere GameObject, the radius of the Collider must be half of the scale of the sphere. If a Sphere has a scale of 2, 2, 2 then the radius would be 1.

What if you need to change just one or two of the values of the scale of the Sphere GameObject? Maybe you want the scale to be X = 1, Y = 2, Z = 1. What happens to the Sphere Collider? Does it still properly cover the GameObject? See below.

No, it does not properly cover it. The Sphere Collider will stay its perfect shape and increase in size, but it will not conform to the oblong shape of the Sphere GameObject. What do you do in this case?

Mesh Collider

This collider allows you to do collision detection between meshes and the primitive shapes Unity has by default. A mesh is like a skin and is the shape of the GameObject. Let us check its component.

Convex- This is the first property. It is used to cover over a mesh / GameObject shape that is not a primitive. It is generally a more complex shape. Remember when changing the Sphere GameObject’s size and the Sphere Collider was not able to take the shape of the Sphere anymore? You can use a Mesh Collider and turn on the Convex property for this.

It covers the oval well. However, notice that there is a warning message in the console. The polygon limit is 256, and using a convex Mesh Collider for this oval shape would have passed that limit. Therefore, the number of polygons was decreased to be within that limit.

Also, notice what happens when the convex property is not enabled:

An error is thrown. According to the error, a “non-convex MeshCollider with non-kinematic Rigidbody is no longer supported since Unity 5.”

To fix this, it continues: “If you want to use a non-convex mesh either make the Rigidbody kinematic or remove the Rigidbody component.”

So basically, if you do not want to use the convex property on the mesh, then the Rigidbody must be kinematic, or you must remove the Rigidbody from the GameObject altogether.

Finally the documentation says this: “…Convex mesh colliders are suitable on Rigidbodies, if you really need more detailed colliders than what the primitive colliders provide you with.” Note that convex Mesh Colliders are more resource intensive and may affect the performance of your game. So you should use primitive colliders as much as possible instead.

Cooking Options – Sounds delicious. This is where a normal mesh is changed to be ready to be used by the physics engine. The physics engine will then be allowed to know where the mesh is along with its size and shape.

By default, “Mixed…” is the selected cooking option. This includes all of the following properties being enabled:

CookForFasterSimulation – Extra checks are done when this is turned on. These checks are to make sure the mesh is in an optimal state of performance while the game is running.

This takes up extra processing power and have a detrimental affect on performance if used a lot. If it is not used, then the mesh will be processed as fast as possible and may not have be perfect.

EnableMeshCleaning – Clean your mesh. This basically does what it says. When enabled, it will do its best to remove imperfect polygons (triangles) that make up the mess, as well as other misshapen polygons. This makes collision detection more accurate.

WeldColocatedVertices – If there are multiple vertices with the same position, this property combines them. This helps with better, more accurate collision feedback.

UseFastMidphase – When enabled, it does not use R-trees. R-trees are structures that are used for keeping information about the position and scale of polygons. Using R-trees can be resource intensive.

Capsule Colliders

A capsule collider, is made of a cylinder that has one half of a sphere on each end. Think of the capsules or pills you take for vitamins and medication.

Now let us check the Capsule Collider Component to see what it’s about. We have already discussed Edit Collider, Is Trigger, Material, Center, and Radius, so we will start at Height.

Height – Height is how tall the whole capsule is. So it is from the tip of the bottom half sphere to the tip of the top half sphere.

Direction – This property dictates which axis the capsule will be positioned length-wise. X, Y, or Z can be picked. Below is a picture showing each.

What is a Compound Collider in Unity?

Finally, you can also add compound colliders to GameObjects. For example, you can start with a Sphere with its own Sphere Collider. Then you can add two cubes and connect them to the sphere in the scene, and also make them a child of the sphere in the Hierarchy. Make sure the two cubes have Box Colliders attached. You now have a Compount Collider.

The second image shows that you have three different colliders fused together. Note that you only need one RigidBody attached to the parent GameObject. The parent GameObject in this case is the sphere.

What is the Difference Between a Collision and a Collider?

It is easy to become confused about the difference between a collision and a collider in Unity. Collision is a class that describes what happen in an actual collision between two GameObjects. When two GameObjects collide, information is stored about the collision in this class. This information can then be passed to functions the Collider class functions, such as OnCollisionEnter(), OnCollisionStay(), and OnCollisionExit().

The Collider class is the base class for colliders, such as the Box Collider and Sphere Collider. The collider attached to a GameObject detects the collision. Remember, the Collision class stores information about the collision. But that information can be passed to the functions mentioned above. Those functions belong to the Collider class.

So the collider first detects the collision. The Collision class holds information about the collision, which can then be passed back to the Collider class that includes functions that can execute code when the collision happens.

Conclusion

There you have the main colliders Unity uses for collisions. You now know the basic information about them. There are several more, Terrain Colliders and Wheel Colliders. Those aren’t used as much so they will be discussed in a separate article.

Make sure you study up on colliders, they are a main concept in Unity. It is important you know how they work to make your own game run properly.

Similar Posts