Adding Colliders

If you don’t want your users to constantly fall through the floor or be able to walk through walls then you will need colliders. A collider is simply a component that makes assets ‘solid’ and unable to be moved through.

Collider Types

The simplest colliders are the box collider, Sphere collider, and Capsule collider. For more complex objects, you can use a Mesh Collider.

The box, sphere, and capsule collider are simple colliders and, therefore, much better for performance.

A mesh collider is more complicated and, therefore, slower in performance. However, there are ways to improve this, and we will go over that later in the tutorial.

Setting up Simple Colliders

  • Select the asset you would like to add the collider to.

  • Go over to the inspector and type the part of the component you want. ‘Box’ or ‘Collider’ is a good example.

  • Modify the collider with the ‘Edit Bounding Volume’ button here. It is also possible to edit the parameters directly if need be.

You can apply a collider on the asset itself or on an empty game object. Adding more than one collider component to an asset is also possible.

If you apply a collider to an empty game object, you can rotate it. Rotation is not possible on the component itself.

Setting up a Mesh Collider

You can add a mesh collider in the same way as other colliders by selecting the asset and adding the Mesh Collider component.

If you apply the Mesh Collider on an asset with a model, the mesh on the asset will be the collider. This is fine for simple objects, but if you have an asset with many polygons, you will want to make a simplified version of the asset so the space has less information to process.

You may also want a different mesh as a collider, such as a ramp instead of stairs, so moving up them will be a smoother experience.

For this, you must make a modified version of the same asset in your asset creation tool. The modified version is purely the collision detection and will not be visible. Make sure to reduce the polygons as much as possible for this.

Once imported into Unity :

  • Drag the mesh (not the asset) into the Mesh slot on the component.

    • Expand the asset in the Project folder, the mesh can be found with a little grid next to it. The asset itself cannot be dragged into the mesh collider slot.

  • You can apply the mesh collider to an empty game object for more control over the asset.

If the Mesh collider doesn’t align with your main mesh, ensure both assets have applied the transform similarly and that the pivot is in the same location in your 3D Creation tool.

You may find that deleting and reapplying the mesh collider component will also work.

Summary

In this tutorial, you have looked at adding colliders to prevent your users from moving through the space's assets.

Last updated