Types of Colliders in Unity. Interaction with RigidBody.

Updated information about this project

This arti­cle belongs to a series that con­sist on mak­ing a first per­son game about find­ing objects inside a maze. It's one of my very first series from the chan­nel. Now I reworked this project and you can down­load it to import in your own Uni­ty project. Some day I will make a new series about this project, sub­scribe to my chan­nel to see all the fresh con­tent about Blender, Uni­ty and pro­gram­ming.

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 arti­cle we'll look at the dif­fer­ent types of Col­lid­ers and how to apply them to our 3D mod­els, as well as how they inter­act with the Rigid­Body component.

Go to the project's Main Page

Video related to this article

🟢 Types of Col­lid­ers in Uni­ty. Inter­ac­tion with RigidBody.


Types of colliders

We start­ed by clear­ing the scene of the GameOb­jects that were left in the pre­vi­ous video. Let's leave the pedestal with the sword and the clock, to be able to ana­lyze the dif­fer­ent types of Colliders.

escena de unity3d se observa un suelo con textura de cesped
Fig. 1: Ini­tial state of the scene to start working.

The sword and the pedestal are two sep­a­rate objects grouped under the same father, so we can select the sword and remove it from the pedestal (fig­ures 2 and 3).

escena en unity3d un pedestal con una espada y un reloj de péndulo
Fig. 2: The GameOb­ject sword is select­ed in the hierarchy.
escena en unity3d un pedestal con una espada y un reloj de péndulo
Fig. 3: The GameOb­ject sword moves away from the pedestal.

In addi­tion we will cre­ate a cube and a sphere by right click­ing on the hier­ar­chy and using the 3D Object menu. We arrange the objects so that they look like in fig­ure 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: Arrange­ment of GameOb­jects to ana­lyze the dif­fer­ent types of Colliders.

With the cube select­ed, we observe its prop­er­ties in the inspec­tor. By default it comes with sev­er­al prop­er­ties includ­ed, among them there is a Box Col­lid­er component.

inspector de un gameobject cubo en unity3d
Fig. 5: View­ing the inspec­tor of a GameOb­ject cube type. 

Com­po­nents can be removed by using the small gear at the top right of a com­po­nent and click­ing Remove Com­po­nent, as shown in Fig­ure 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 cor­ner to remove a component.

In the fol­low­ing fig­ure we see that the Box Col­lid­er com­po­nent has been removed.

inspector de un gameobject cubo sin box collider en unity3d
Fig. 7: The Box Col­lid­er has been removed from the components.

We can add new com­po­nents to our GameOb­jects by click­ing on the "Add Com­po­nent" but­ton, when we do so a win­dow is dis­played with all the com­po­nents and has a search bar. If we type "Col­lid­er" in the search bar we can see the dif­fer­ent types of Col­lid­er we can add to our GameObject.

Of course there are Col­lid­ers that are more appro­pri­ate than oth­ers for a par­tic­u­lar GameOb­ject, this will depend on the geom­e­try of the GameOb­ject and the use we are going to give it.

MOST SEARCHED VIDEOS FROM MY CHANNEL

ABOUT UNITY

ABOUT BLENDER

inspector de un gameobject cubo en unity3d. el menu add component está desplegado.
Fig. 8: Press­ing the Add Com­po­nent but­ton dis­plays a list of all the com­po­nents that can be added to the GameObject.

Let's deac­ti­vate the MeshRen­der­er com­po­nent of the cube to bet­ter observe the Col­lid­er, the com­po­nents usu­al­ly bring a small box to the left of their name to acti­vate or deac­ti­vate them.

In fig­ure 9 we see that the Box Col­lid­er is rep­re­sent­ed as a Cube defined by green edges. Because the GameOb­ject is a cube, this type of Col­lid­er fits per­fect­ly 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: Switch­ing off the Mesh Ren­der­er com­po­nent of the GameOb­ject cube dis­plays the Box Collider.

Now let's select in the hier­ar­chy the GameOb­ject sphere and see its prop­er­ties in the inspector.

This time he's assigned a type of spher­i­cal Collider.

inspector de un gameobject esfera en unity3d
Fig. 10: View­ing the inspec­tor of a sphere type GameObject. 

Again we deac­ti­vate the Mesh Ren­der­er com­po­nent to observe the Col­lid­er (fig­ure 11). Again, as it is a spher­i­cal GameOb­ject the type of spher­i­cal Col­lid­er fits per­fect­ly 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: Turn­ing off the Mesh Ren­der­er com­po­nent of the GameOb­ject sphere dis­plays the Sphere Collider. 

Let's see what hap­pens with the sword, select it in the hier­ar­chy and observe its prop­er­ties in the inspec­tor. Unlike the two pre­vi­ous GameOb­jects, the sword is not assigned a Col­lid­er by default, we must assign one man­u­al­ly as shown in Fig­ure 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 pre­vi­ous­ly moved from the pedestal.

First we assign a Box Col­lid­er. In this case as shown in fig­ure 13, we see that a box appears with the min­i­mum vol­ume nec­es­sary to ful­ly con­tain the GameOb­ject 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: GameOb­ject sword with a Box Col­lid­er assigned.

Then we tried a kind of spher­i­cal Col­lid­er. As shown in Fig­ure 14, the Col­lid­er is a sphere with the min­i­mum radius such that the sphere com­plete­ly con­tains the GameOb­ject 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: GameOb­ject sword with a Sphere Col­lid­er assigned. 

Now let's add a Mesh Col­lid­er, this type of Col­lid­er will use the geom­e­try of the object. Fig­ure 15 shows its prop­er­ties in the inspector.

visualizacion del inspector de un gameobject en unity3d
Fig. 15: A Mesh Col­lid­er com­po­nent has been added to the GameOb­ject sword.

We deac­ti­vate the Mesh Ren­der­er to observe the Col­lid­er and what we see is that it has the exact shape of the sword, the Col­lid­er coin­cides with the geo­met­ric mesh of triangles.

visualización de un mesh collider aplicado a un gameobject espada. mesh renderer desactivado.
Fig. 16: By turn­ing off the Mesh Ren­der­er com­po­nent of the GameOb­ject sword, we can visu­al­ize the Mesh Collider.

Of course this brings more com­plex­i­ty in the cal­cu­la­tions of inter­ac­tions with this type of Col­lid­er, so we have to decide if it is nec­es­sary to use it, or with a Box Col­lid­er is not enough. Every­thing 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 Col­lid­er has a com­plex geom­e­try, which trans­lates into more com­pu­ta­tion time.

Next we'll select the GameOb­ject clock in the hier­ar­chy and apply a Mesh Col­lid­er but this time we'll click on the "Con­vex" box that is seen in the prop­er­ties of the Col­lid­er in Fig­ure 18.

visualización del inspector de un gameobject en unity 3d.
Fig. 18: A Mesh Col­lid­er has been added to the GameOb­ject Clock and the Con­vex box has been checked.

In fig­ure 19 we observe that now the Col­lid­er type par­tial­ly respects the geom­e­try of the object. This type of Col­lid­er also involves more cal­cu­la­tions, but not as much as it would be in the pre­vi­ous case, is as if we had a low poly mod­el of the GameObject.

It also has oth­er advan­tages such as being able to use it in Trig­ger mode.

reloj de péndulo de oro recubiero por un mesh collider convexo en unity3d. suelo de cesped con margaritas.
Fig. 19: The Mesh Col­lid­er is dis­played in con­vex mode.

For now we saw three types of Col­lid­ers: Box Col­lid­er, Sphere Col­lid­er and Mesh Col­lid­er, the lat­ter can be used in Con­vex mode, which sim­pli­fies the geom­e­try of the Collider.

cuatro tipos de colliders, fondo negro.
Fig. 20: Overview of Col­lid­er types.

Physical behavior of the Colliders.

The Col­lid­ers behave like bar­ri­ers that can't be crossed. In oth­er words, if our char­ac­ter has a Col­lid­er and tries to go through a GameOb­ject assigned to a Col­lid­er (such as a cube or a clock), he won't be able to do it. Unless this Col­lid­er is in Trig­ger mode.

Fig­ure 21 shows that the char­ac­ter is stand­ing on the clock, the spher­i­cal Col­lid­er pre­vents it from falling.

modo juego de un proyecto en unity3d
Fig. 21: The play­er is stand­ing on the clock. The assigned Mesh Col­lid­er pre­vents the play­er from falling through it.

Now we are going to place the sphere sus­pend­ed in the air over the cube (the lat­ter 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 sus­pend­ed on the hub.

With the sphere select­ed click on Add Com­po­nent and with­in the Physics cat­e­go­ry select the Rigid­Body component.

visualización del inspector de un gameobject en unity 3d. menu add component desplegado, componente rigidbody seleccionada.
Fig. 23: A Rigid­Body com­po­nent is added to the sphere.

Fig­ure 24 shows the para­me­ters of the Rigid­Body com­po­nent. We can assign a mass, acti­vate or deac­ti­vate grav­i­ty and mod­i­fy oth­er para­me­ters relat­ed to the physics of an object.

visualización de la componente rigidbody de un gameobject en unity 3d.
Fig. 24: Dis­play of Rigid­Body com­po­nent para­me­ters in the inspector.

When enter­ing the game mode we see that the sphere falls as if by grav­i­ty and col­lides with the cor­ner of the cube (actu­al­ly the col­li­sion is with the Collider).

The Rigid­Body com­po­nent gives phys­i­cal behav­iour to the sphere.

modo juego de un proyecto en unity3d. esfera cae por gravedad sobre cubo.
Fig. 25: The ball has fall­en due to the grav­i­ta­tion­al behav­iour of the Rigid­Body component.

Add Colliders to the labyrinth pieces

We are going to place one by one all the pre­fab­ri­cat­ed ones cre­at­ed in the pre­vi­ous video and con­fig­ur­ing their Col­lid­ers so that the play­er can't go through the walls.

visualización del programa unity, proyecto de juego de laberinto.
Fig. 26: We open the fold­er where you can find the pre­fab­ri­cat­ed prod­ucts that were made in the pre­vi­ous video.

First the closed alley piece. We take the Pre­fab from the project fold­er and drag it to the scene or to the hier­ar­chy. Then in the inspec­tor click on the Add Com­po­nent but­ton and choose the Box Col­lid­er 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 fig­ure 28 that the Col­lid­er com­plete­ly cov­ers the GameOb­ject, 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 Col­lid­er has been assigned to the part.

Fig­ure 29 shows that in the game mode the char­ac­ter can only advance to the bor­der of the Box Col­lid­er. This type of Col­lid­er is not suit­able 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 play­er can­not enter the space between the walls because he is obstruct­ed by the Box Collider.

We have an alter­na­tive, if we click on the but­ton "Edit Col­lid­er" of the Box Col­lid­er (fig­ure 30), we will be able to mod­i­fy the posi­tion 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 Col­lid­er but­ton you can edit the dimen­sions of the Collider. 

This way we can make the Box Col­lid­er 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 Col­lid­er enclose a part of the object.

Then adding more Col­lid­ers and repeat­ing the pre­vi­ous process for the rest of the geom­e­try of the object, we would be able to rep­re­sent with suf­fi­cient pre­ci­sion the bor­der 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 Col­lid­ers and edit.

Fig­ure 33 shows that the super­po­si­tion of the three Box Col­lid­ers cov­ers the entire geom­e­try of the piece and allows the play­er 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 Col­lid­ers the fron­tier of the GameOb­ject is represented.

The above works for us, but let's elim­i­nate all the Box Col­lid­ers and add a Mesh Col­lid­er instead, in this case the wall geom­e­try 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 Ren­der­er com­po­nent is deac­ti­vat­ed, the Mesh Col­lid­er com­po­nent can be seen.

Some­thing very impor­tant when we are deal­ing with Pre­fabs is to apply the changes we are mak­ing, for that we use the Apply but­ton locat­ed at the top of the inspec­tor (if we have the Pre­fab selected).

visualización del inspector de un gameobject en unity 3d.
Fig. 35: It is nec­es­sary to apply the changes that are made to a Prefab.

Fig­ure 36 shows the mod­el of the por­tal with an assigned Mesh Col­lid­er 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 Col­lid­er assigned to the door. Com­plex geometry.

It is not jus­ti­fied to use a Mesh Col­lid­er for this mod­el, tak­ing into account the use that we are going to give it, let's put bet­ter a Box Col­lid­er 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 Col­lid­er assigned to the door.

The same for the pedestal mod­el, with a Box Col­lid­er 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 Col­lid­er assigned to the pedestal with the sword.

The clocks are going to be col­lectible objects, that is to say when the play­er pass­es over them, these will dis­ap­pear and will give more time of game.

Bear­ing in mind the use that we are going to give them, we are going to use a spher­i­cal Col­lid­er, increas­ing a lit­tle the radius as it is observed in fig­ure 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 Col­lid­er is assigned in Trig­ger mode.

Then let's acti­vate the "Is Trig­ger" option of the Sphere Col­lid­er com­po­nent. This will ensure that the Col­lid­er does not hin­der the play­er and allows him to detect when he enters the spher­i­cal region.

visualización de las propiades de un sphere collider en un proyecto en unity 3d. is trigger activado. juego de laberinto.
Fig. 40: Acti­vat­ing the "Is Trig­ger" box indi­cates that the Col­lid­er will act as a Trigger.

Conclusion

In this arti­cle we look at dif­fer­ent types of Col­lid­ers and what each may be appro­pri­ate for.

Every­thing depends on the pur­pose that we have for the GameOb­ject, for exam­ple in the pieces of the labyrinth we use Mesh Col­lid­er because we need to be able to walk through the inner regions of the mod­el. On the oth­er hand in the clocks we use a spher­i­cal Col­lid­er because it is a piece that the char­ac­ter will be able to grasp.

Col­lid­ers inter­act with each oth­er and are very impor­tant when Rigid­Body com­po­nents come into play, which give GameOb­ject phys­i­cal behaviour.

Scroll to Top
Secured By miniOrange