#3 Types of Colliders. Interaction with RigidBody.

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

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.

escena de unity3d se observa un suelo con textura de cesped
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).

escena en unity3d un pedestal con una espada y un reloj de péndulo
Fig. 2: The GameObject sword is selected in the hierarchy.
escena en unity3d un pedestal con una espada y un reloj de péndulo
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.

escena en unity3d se observa un suelo con textura de cesped con margaritas. una espada, un cubo blanco y una esfera blanca.
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.

inspector de un gameobject cubo en unity3d
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.

inspector de un gameobject cubo en unity3d. aparece un menú que permite eliminar el componente
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.

inspector de un gameobject cubo sin box collider en unity3d
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.

inspector de un gameobject cubo en unity3d. el menu add component está desplegado.
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.

una escena de unity 3d con un suelo con una textura de cesped con margaritas, se observa una esfera blanca y aristas color verde de un cubo.
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.

inspector de un gameobject esfera en unity3d
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.

escena en unity3d con un suelo con textura de cesped con margaritas. se observa una esfera definida por líneas color verde. collider esférico.
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.

escena en unity 3d suelo con textura de cesped con margaritas, una espada apuntando hacia abajo y medio reloj.
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.

escena en unity 3d con suelo con textura de cesped con margaritas, se observa una espada seleccionada con un box collider que la recubre.
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.

escena en unity 3d con suelo con textura de cesped con margaritas, se observa una espada seleccionada con un sphere collider que la recubre. se observa un reloj de péndulo de oro.
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.

visualizacion del inspector de un gameobject en unity3d
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.

visualización de un mesh collider aplicado a un gameobject espada. mesh renderer desactivado.
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.

se observa la geometría de un collider tipo mesh collider en unity 3d.
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.

visualización del inspector de un gameobject en unity 3d.
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.

reloj de péndulo de oro recubiero por un mesh collider convexo en unity3d. suelo de cesped con margaritas.
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.

cuatro tipos de colliders, fondo negro.
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.

modo juego de un proyecto en unity3d
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).

se observa una esfera blanca suspendida en el aire sobre un cubo blanco, cesped con margaritas. atardecer nublado.
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.

visualización del inspector de un gameobject en unity 3d. menu add component desplegado, componente rigidbody seleccionada.
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.

visualización de la componente rigidbody de un gameobject en unity 3d.
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.

modo juego de un proyecto en unity3d. esfera cae por gravedad sobre cubo.
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.

visualización del programa unity, proyecto de juego de laberinto.
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.

pieza de laberinto colocada en la escena de un proyecto en unity 3d. juego de laberinto.
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.

pieza con box collider de laberinto colocada en la escena de un proyecto en unity 3d. juego de laberinto..
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.

pieza de laberinto colocada en la escena de un proyecto en unity 3d. juego de laberinto. modo juego, el personaje no puede atravesar el collider.
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.

visualización de la componente box collider de un gameobject, se ha apretado el botón edit collider.
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.

pieza con box collider de laberinto colocada en la escena de un proyecto en unity 3d. se cambia las dimensiones del box collider. juego de laberinto.
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.

visualización del inspector de un gameobject en unity 3d. tres componentes box collider asignadas.
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.

pieza de laberinto con tres box colliders colocada en la escena de un proyecto en unity 3d. juego de laberinto.
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.

visualización del mesh collider de una pieza de laberinto de un proyecto en unity3d. juego de laberinto.
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).

visualización del inspector de un gameobject en unity 3d.
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.

visualización del mesh collider de un gameobject en un proyecto en unity3d. El gameobject es una puerta con muchos polígonos.
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.

escena de un proyecto en unity 3d. suelo de cesped con margarita, portal antiguo de piedras y madera. box collider asignado. juego de laberinto.
Fig. 37: Box Collider assigned to the door.

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

box collider asignado a un gameobject que es un pedestal de piedra con una espada incrustada. suelo de cesped con margarita. juego de laberinto en unity 3d.
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.

sphere collider asignado a un gameobject que es un reloj de péndulo de oro. suelo de cesped con margaritas. juego de laberinto en unity 3d.
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.

visualización de las propiades de un sphere collider en un proyecto en unity 3d. is trigger activado. juego de laberinto.
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.

Leave a Comment

Your email address will not be published. Required fields are marked *