Collider
TypeClass (Component, base class)
NamespaceUnityEngine
InheritsComponent > Object
SubclassesBoxCollider, SphereCollider, CapsuleCollider, MeshCollider, TerrainCollider, CharacterController
2D versionCollider2D

Collider

A Collider gives an object an invisible physical shape. It's what raycasts hit, what Rigidbodies bump into, and what triggers detect. The shape doesn't have to match the visible mesh exactly; simple shapes are faster.

Collider types

TypeShapeSpeedUse for
BoxColliderBoxFastCrates, walls, buildings
SphereColliderBallFastestBalls, pickup ranges
CapsuleColliderPillFastCharacters
MeshColliderMatches a meshSlowComplex static level geometry. Needs Convex on for dynamic Rigidbodies.

For complex moving objects, add several primitive colliders on child objects (a compound collider) instead of one Mesh Collider.

Properties

NameTypeDescription
enabledboolTurn the collider on/off.
isTriggerboolIf true, nothing bounces off it; it only reports when things enter/exit.
boundsBoundsThe world-space box that fully contains the collider.
attachedRigidbodyRigidbodyThe Rigidbody it belongs to (can be on a parent), or null.
sharedMaterialPhysicsMaterialFriction and bounciness. (Called PhysicMaterial before Unity 6.)

Methods

MethodReturnsDescription
ClosestPoint(Vector3 position)Vector3The point on (or in) the collider closest to a position.
Raycast(Ray ray, out RaycastHit hit, float maxDistance)boolRaycast against only this collider.

Events (messages on your scripts)

Write these methods on a script on the same GameObject as the collider (or on the GameObject with the Rigidbody).

MessageParameterCalled when
OnCollisionEnterCollisionStarted touching (solid).
OnCollisionStayCollisionEvery physics step while touching.
OnCollisionExitCollisionStopped touching.
OnTriggerEnterColliderSomething entered a trigger.
OnTriggerStayColliderEvery physics step while inside.
OnTriggerExitColliderSomething left a trigger.

Useful members of Collision

MemberDescription
collision.gameObjectThe object we hit.
collision.colliderThe exact collider we hit.
collision.relativeVelocityHow fast the two objects hit each other.
collision.GetContact(0).pointWhere they touched (world space).
collision.GetContact(0).normalThe surface direction at that point (points away from the surface).

Code Samples

Damage zone (trigger)

using UnityEngine;

public class LavaZone : MonoBehaviour
{
    [SerializeField] private int damagePerSecond = 20;

    void OnTriggerStay(Collider other)
    {
        if (other.TryGetComponent(out Health health))
        {
            // OnTriggerStay runs each physics step, so scale by the step length.
            health.TakeDamage(damagePerSecond * Time.fixedDeltaTime);
        }
    }
}

Spawn sparks where a bullet hits

using UnityEngine;

public class ImpactEffect : MonoBehaviour
{
    [SerializeField] private GameObject sparksPrefab;

    void OnCollisionEnter(Collision collision)
    {
        ContactPoint contact = collision.GetContact(0);
        // Point the sparks out of the surface.
        Quaternion rot = Quaternion.LookRotation(contact.normal);
        Instantiate(sparksPrefab, contact.point, rot);
        Destroy(gameObject);
    }
}

Ground check with a trigger under the feet

using UnityEngine;

// Put this on a small trigger collider child object at the character's feet.
public class GroundSensor : MonoBehaviour
{
    public bool IsGrounded => contacts > 0;
    private int contacts;

    void OnTriggerEnter(Collider other) { if (!other.isTrigger) contacts++; }
    void OnTriggerExit(Collider other)  { if (!other.isTrigger) contacts--; }
}

What happens at runtime

  1. Each physics step, the engine checks which colliders overlap or touch.
  2. For solid collisions, it pushes Rigidbodies apart and adjusts their velocity (friction, bounce).
  3. It compares this step's contacts with the last step's: new pairs get Enter, continuing pairs get Stay, ended pairs get Exit.
  4. Messages are sent to scripts on both objects. On a child collider of a Rigidbody, messages go to the Rigidbody's GameObject.
  5. Disabling or destroying a collider while inside a trigger does not call OnTriggerExit. Keep that in mind for counters like the ground sensor above.

Common Mistakes

See also