Understanding Unity Colliders

Colliders - Unity Official Tutorials

Estimated read time: 1:20

    Learn to use AI like a Pro

    Get the latest AI workflows to boost your productivity and business performance, delivered weekly by expert consultants. Enjoy step-by-step guides, weekly Q&A sessions, and full access to our AI workflow archive.

    Canva Logo
    Claude AI Logo
    Google Gemini Logo
    HeyGen Logo
    Hugging Face Logo
    Microsoft Logo
    OpenAI Logo
    Zapier Logo
    Canva Logo
    Claude AI Logo
    Google Gemini Logo
    HeyGen Logo
    Hugging Face Logo
    Microsoft Logo
    OpenAI Logo
    Zapier Logo

    Summary

    Unity's colliders are essential components that enable game objects to interact with each other. These components come in various primitive shapes, such as spheres, capsules, and boxes, or can be customized using mesh colliders. For complex objects, combining simpler shapes or using alternate, less complex geometry for collision meshes can enhance performance without sacrificing collision accuracy. Collision events such as OnCollisionEnter, OnCollisionStay, and OnCollisionExit are crucial for determining interactions and require at least one object involved to have a rigidbody component to function.

      Highlights

      • Unity's colliders can be primitive shapes or mesh colliders. 🎮
      • For performance, avoid overly detailed mesh colliders, opt for compound shapes. 🚀
      • OnCollisionEnter triggers when colliders first intersect. 📍
      • OnCollisionStay reports ongoing collisions. 🌟
      • OnCollisionExit signals when colliders stop touching. ✋

      Key Takeaways

      • Colliders allow objects to interact by detecting collisions. 🤝
      • Primitive shapes include spheres, capsules, and boxes. 🔲
      • Mesh colliders fit exactly to the mesh shape, but may affect performance. 🚀
      • Combining primitive shapes can optimize complex models' collisions. ⚙️
      • Collision events are OnCollisionEnter, OnCollisionStay, and OnCollisionExit. 🔄

      Overview

      In the world of Unity development, colliders are a foundational tool for enabling game objects to detect and react to contact with other objects. They come predominantly as primitive shapes like spheres, boxes, and capsules, or as mesh colliders that map to the exact shape of your model. While mesh colliders offer precise collision boundaries, they can be performance-heavy, so combining primitive shapes often provides a more efficient solution.

        A fantastic middle-ground is using simplified meshes for creating collision boundaries. For instance, if you have a highly detailed model, instead of using a mesh collider directly on it, you can create a secondary, less complex geometry. This ensures that the game runs smoothly while maintaining the necessary accuracy for collision detection. The decision between using compound colliders or mesh colliders heavily depends on the complexity and performance demands of your game.

          Handling collision events is also an integral part of utilizing colliders. Unity executes specific actions based on the stage of collision detected, such as OnCollisionEnter when a collision starts, OnCollisionStay as long as the collision is ongoing, and OnCollisionExit once colliders separate. These are essential for programming responsive game mechanics and require at least one of the colliding objects to have a rigidbody.

            Chapters

            • 00:00 - 00:30: Introduction to Colliders Colliders are elements in a game that allow interactions between game objects when at least one has a rigidbody. They appear in different shapes like spheres, capsules, and boxes, marked in the scene by a green outline.
            • 00:30 - 01:30: Compound and Mesh Colliders For more complex shapes, there are two main options for colliders: combining primitive shapes or using a mesh collider. Primitive colliders can be applied to different objects in a hierarchy to cover various areas, as demonstrated with the workbench example. Alternatively, a mesh collider fits the exact shape of a specified mesh, useful when no hierarchy is needed.
            • 01:30 - 02:30: Using Simpler Geometry for Collisions The chapter discusses the drawbacks of using a mesh collider directly tied to a detailed model for collision detection, as it can negatively impact performance due to the complexity of the mesh. Instead, it recommends a simpler approach by using a compound setup or creating separate, simpler geometry specifically for collisions while still utilizing a mesh collider. This helps in balancing detail and performance in collision detection systems.
            • 02:30 - 04:00: Collision Events and Scripts This chapter discusses the use of collision meshes in the context of a robot arm asset. The original detailed mesh is too complex for collision purposes, so a secondary set of simpler geometry has been built for the purpose of mesh colliders. Parts of the model, like the claw, have been simplified in this separate set of geometry. These designs are maintained in two separate FBX files, one for the original artwork and one for the simplified collision geometry.
            • 04:00 - 05:00: Collision Event Demonstration In this chapter titled 'Collision Event Demonstration', the focus is on the application and benefits of mesh colliders in game development. By applying a mesh collider and using these meshes for collision, developers can achieve high accuracy in collision detection without significant performance costs. During a collision event in the game engine, when one collider hits another, an event known as OnCollisionEnter is triggered. An example given is the 'prop samoflange' object, which is equipped with a sphere collider component and a rigidbody component, the latter providing mass and gravity.

            Colliders - Unity Official Tutorials Transcription

            • 00:00 - 00:30 Colliders are a component that allows the game object they're attached to to react to other colliders provided that one of the game objects has a rigidbody component attached. Colliders come in various shapes and types, and are denoted in the scene view by a green outline. They can have the following primitive shapes: a sphere, a capsule and a box.
            • 00:30 - 01:00 For more complex shapes you have two options You can either combine several of these primitive shapes together by applying primitive colliders to different objects. in our hierarchy. For example this workbench has a number of objects which simply serve to make up it's different colliders for various areas. The other option is to use a mesh collider, which will fit the exact shape of the mesh that you specify. The workbench on the right has no hierarchy
            • 01:00 - 01:30 but instead uses a mesh collider. The reason not to use a mesh collider is that it will fit the exact shape of the mesh that you specify. So if you only specify the mesh of your detailed model then it may be providing too detailed a collision mesh and effecting performance. This is the reason why it's often better to make a compound setup instead. However it should be noted that a third option for creating collision geometry is to use a separate, simpler set of geometry and still use a mesh collider. In this example we have this complex
            • 01:30 - 02:00 robot arm asset. It's a very detailed mesh but we don't want a mesh as complex as this for the collisions. So what we've done is built a secondary set of geometry which we've then applied to a series of mesh colliders. For example this part of the claw is more detailed than the collision mesh that we've created for it. And this has been done in two separate FBX files -The original artwork and a simplified set of geometry.
            • 02:00 - 02:30 We've then gone through each one, applied a mesh collider and dragged these meshes over as the collision meshes to use. This means that we get the kind of accuracy that we need in terms of collision without the performance overhead. When collisions occur in the game engine one collider strikes another and an event called OnCollisionEnter is called. In this scene our 'prop samoflange' object has a sphere collider component and a rigidbody component. The rigidbody provides mass and gravity.
            • 02:30 - 03:00 When I play the game, one falls down and strikes the other. The power cube has a box collider attached to it. Also attached to our falling object is this script. This script checks for three collision events. OnCollisionEnter, OnCollisionStay and OnCollisionExit. When each of these occurs it writes to the console using Debug.Log. It will register when Enter is called,
            • 03:00 - 03:30 when Stay is occurring and when Exit is called. So if we look at our console you can see that Enter is called, Stay has occurred for a while and then Exit is called. If we pause the game and play we can look at this example slowly. As I step through the frames when the collision occurs you can see that Enter is called, so OnCollisionEnter
            • 03:30 - 04:00 has just occurred. As I continue OnCollisionStay is occurring. You can see on the right here that it's happening several times because these two colliders are still in contact. As we continue to step through eventually OnCollisionExit is called when the two colliders are no longer in contact. Note that for an OnCollision message to to be sent, one of the two objects colliding must have a rigidbody component.
            • 04:00 - 04:30 Subtitles by the Amara.org community