Types of Colliders in Unity. Interaction with RigidBody.

Updated information about this project

This article belongs to a series that consist on making a first person game about finding objects inside a maze. It’s one of my very first series from the channel. Now I reworked this project and you can download it to import in your own Unity project. Some day I will make a new series about this project, subscribe to my channel to see all the fresh content about Blender, Unity and programming.

Follow me on itch.io and download the source code of this project

YOU CAN TRY THIS GAME HERE, IT MAY TAKE A LITTLE WHILE TO LOAD
🔻

MOVEMENT: WASD CAMERA LOOK: MOUSE

Introduction of the old article

In this article we’ll look at the different types of Colliders and how to apply them to our 3D models, as well as how they interact with the RigidBody component.

Go to the project’s Main Page

Video related to this article – Types of Colliders in Unity. Interaction with RigidBody.


Types of colliders

We started by clearing the scene of the GameObjects that were left in the previous video. Let’s leave the pedestal with the sword and the clock, to be able to analyze the different types of Colliders.

Fig. 1: Initial state of the scene to start working.

The sword and the pedestal are two separate objects grouped under the same father, so we can select the sword and remove it from the pedestal (figures 2 and 3).

Fig. 2: The GameObject sword is selected in the hierarchy.
Fig. 3: The GameObject sword moves away from the pedestal.

In addition we will create a cube and a sphere by right clicking on the hierarchy and using the 3D Object menu. We arrange the objects so that they look like in figure 4.

Fig. 4: Arrangement of GameObjects to analyze the different types of Colliders.

With the cube selected, we observe its properties in the inspector. By default it comes with several properties included, among them there is a Box Collider component.

Fig. 5: Viewing the inspector of a GameObject cube type.

Components can be removed by using the small gear at the top right of a component and clicking Remove Component, as shown in Figure 6.

Fig. 6: Click on the gear in the upper right corner to remove a component.

In the following figure we see that the Box Collider component has been removed.

Fig. 7: The Box Collider has been removed from the components.

We can add new components to our GameObjects by clicking on the “Add Component” button, when we do so a window is displayed with all the components and has a search bar. If we type “Collider” in the search bar we can see the different types of Collider we can add to our GameObject.

Of course there are Colliders that are more appropriate than others for a particular GameObject, this will depend on the geometry of the GameObject and the use we are going to give it.

Fig. 8: Pressing the Add Component button displays a list of all the components that can be added to the GameObject.

Let’s deactivate the MeshRenderer component of the cube to better observe the Collider, the components usually bring a small box to the left of their name to activate or deactivate them.

In figure 9 we see that the Box Collider is represented as a Cube defined by green edges. Because the GameObject is a cube, this type of Collider fits perfectly into its geometry.

Fig. 9: Switching off the Mesh Renderer component of the GameObject cube displays the Box Collider.

Now let’s select in the hierarchy the GameObject sphere and see its properties in the inspector.

This time he’s assigned a type of spherical Collider.

Fig. 10: Viewing the inspector of a sphere type GameObject.

Again we deactivate the Mesh Renderer component to observe the Collider (figure 11). Again, as it is a spherical GameObject the type of spherical Collider fits perfectly to its geometry.

Fig. 11: Turning off the Mesh Renderer component of the GameObject sphere displays the Sphere Collider.

Let’s see what happens with the sword, select it in the hierarchy and observe its properties in the inspector. Unlike the two previous GameObjects, the sword is not assigned a Collider by default, we must assign one manually as shown in Figure 8.

Fig. 12: Select the sword that was previously moved from the pedestal.

First we assign a Box Collider. In this case as shown in figure 13, we see that a box appears with the minimum volume necessary to fully contain the GameObject sword.

Fig. 13: GameObject sword with a Box Collider assigned.

Then we tried a kind of spherical Collider. As shown in Figure 14, the Collider is a sphere with the minimum radius such that the sphere completely contains the GameObject sword.

Fig. 14: GameObject sword with a Sphere Collider assigned.

Now let’s add a Mesh Collider, this type of Collider will use the geometry of the object. Figure 15 shows its properties in the inspector.

Fig. 15: A Mesh Collider component has been added to the GameObject sword.

We deactivate the Mesh Renderer to observe the Collider and what we see is that it has the exact shape of the sword, the Collider coincides with the geometric mesh of triangles.

Fig. 16: By turning off the Mesh Renderer component of the GameObject sword, we can visualize the Mesh Collider.

Of course this brings more complexity in the calculations of interactions with this type of Collider, so we have to decide if it is necessary to use it, or with a Box Collider is not enough. Everything will depend on the purpose.

Fig. 17: It is observed that the Mesh Collider has a complex geometry, which translates into more computation time.

Next we’ll select the GameObject clock in the hierarchy and apply a Mesh Collider but this time we’ll click on the “Convex” box that is seen in the properties of the Collider in Figure 18.

Fig. 18: A Mesh Collider has been added to the GameObject Clock and the Convex box has been checked.

In figure 19 we observe that now the Collider type partially respects the geometry of the object. This type of Collider also involves more calculations, but not as much as it would be in the previous case, is as if we had a low poly model of the GameObject.

It also has other advantages such as being able to use it in Trigger mode.

Fig. 19: The Mesh Collider is displayed in convex mode.

For now we saw three types of Colliders: Box Collider, Sphere Collider and Mesh Collider, the latter can be used in Convex mode, which simplifies the geometry of the Collider.

Fig. 20: Overview of Collider types.

Physical behavior of the Colliders.

The Colliders behave like barriers that can’t be crossed. In other words, if our character has a Collider and tries to go through a GameObject assigned to a Collider (such as a cube or a clock), he won’t be able to do it. Unless this Collider is in Trigger mode.

Figure 21 shows that the character is standing on the clock, the spherical Collider prevents it from falling.

Fig. 21: The player is standing on the clock. The assigned Mesh Collider prevents the player from falling through it.

Now we are going to place the sphere suspended in the air over the cube (the latter was scaled).

Fig. 22: The sphere is suspended on the hub.

With the sphere selected click on Add Component and within the Physics category select the RigidBody component.

Fig. 23: A RigidBody component is added to the sphere.

Figure 24 shows the parameters of the RigidBody component. We can assign a mass, activate or deactivate gravity and modify other parameters related to the physics of an object.

Fig. 24: Display of RigidBody component parameters in the inspector.

When entering the game mode we see that the sphere falls as if by gravity and collides with the corner of the cube (actually the collision is with the Collider).

The RigidBody component gives physical behaviour to the sphere.

Fig. 25: The ball has fallen due to the gravitational behaviour of the RigidBody component.

Add Colliders to the labyrinth pieces

We are going to place one by one all the prefabricated ones created in the previous video and configuring their Colliders so that the player can’t go through the walls.

Fig. 26: We open the folder where you can find the prefabricated products that were made in the previous video.

First the closed alley piece. We take the Prefab from the project folder and drag it to the scene or to the hierarchy. Then in the inspector click on the Add Component button and choose the Box Collider component.

Fig. 27: We place the piece “Dead End” in the scene.

We see in figure 28 that the Collider completely covers the GameObject, which will not allow us to walk inside it.

Fig. 28: A Box Collider has been assigned to the part.

Figure 29 shows that in the game mode the character can only advance to the border of the Box Collider. This type of Collider is not suitable for this piece.

Fig. 29: The player cannot enter the space between the walls because he is obstructed by the Box Collider.

We have an alternative, if we click on the button “Edit Collider” of the Box Collider (figure 30), we will be able to modify the position of its faces.

Fig. 30: Using the Edit Collider button you can edit the dimensions of the Collider.

This way we can make the Box Collider enclose a part of the object.

This way we can make the Box Collider enclose a part of the object.

Then adding more Colliders and repeating the previous process for the rest of the geometry of the object, we would be able to represent with sufficient precision the border of the object.

Fig. 32: We add two more Box Colliders and edit.

Figure 33 shows that the superposition of the three Box Colliders covers the entire geometry of the piece and allows the player to move around inside.

Fig. 33: Using three Box Colliders the frontier of the GameObject is represented.

The above works for us, but let’s eliminate all the Box Colliders and add a Mesh Collider instead, in this case the wall geometry and quite simple.

Fig. 34: When the Mesh Renderer component is deactivated, the Mesh Collider component can be seen.

Something very important when we are dealing with Prefabs is to apply the changes we are making, for that we use the Apply button located at the top of the inspector (if we have the Prefab selected).

Fig. 35: It is necessary to apply the changes that are made to a Prefab.

Figure 36 shows the model of the portal with an assigned Mesh Collider component.

Fig. 36: Mesh Collider assigned to the door. Complex geometry.

It is not justified to use a Mesh Collider for this model, taking into account the use that we are going to give it, let’s put better a Box Collider and apply the changes to the Prefab.

Fig. 37: Box Collider assigned to the door.

The same for the pedestal model, with a Box Collider we get enough.

Fig. 38: Box Collider assigned to the pedestal with the sword.

The clocks are going to be collectible objects, that is to say when the player passes over them, these will disappear and will give more time of game.

Bearing in mind the use that we are going to give them, we are going to use a spherical Collider, increasing a little the radius as it is observed in figure 39.

Fig. 39: A Sphere Collider is assigned in Trigger mode.

Then let’s activate the “Is Trigger” option of the Sphere Collider component. This will ensure that the Collider does not hinder the player and allows him to detect when he enters the spherical region.

Fig. 40: Activating the “Is Trigger” box indicates that the Collider will act as a Trigger.

Conclusion

In this article we look at different types of Colliders and what each may be appropriate for.

Everything depends on the purpose that we have for the GameObject, for example in the pieces of the labyrinth we use Mesh Collider because we need to be able to walk through the inner regions of the model. On the other hand in the clocks we use a spherical Collider because it is a piece that the character will be able to grasp.

Colliders interact with each other and are very important when RigidBody components come into play, which give GameObject physical behaviour.

Exit mobile version
Secured By miniOrange