Home › API Reference › Collider
Collider
| Type | Class (Component, base class) |
| Namespace | UnityEngine |
| Inherits | Component > Object |
| Subclasses | BoxCollider, SphereCollider, CapsuleCollider, MeshCollider, TerrainCollider, CharacterController |
| 2D version | Collider2D |
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
| Type | Shape | Speed | Use for |
| BoxCollider | Box | Fast | Crates, walls, buildings |
| SphereCollider | Ball | Fastest | Balls, pickup ranges |
| CapsuleCollider | Pill | Fast | Characters |
| MeshCollider | Matches a mesh | Slow | Complex 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
| Name | Type | Description |
enabled | bool | Turn the collider on/off. |
isTrigger | bool | If true, nothing bounces off it; it only reports when things enter/exit. |
bounds | Bounds | The world-space box that fully contains the collider. |
attachedRigidbody | Rigidbody | The Rigidbody it belongs to (can be on a parent), or null. |
sharedMaterial | PhysicsMaterial | Friction and bounciness. (Called PhysicMaterial before Unity 6.) |
Methods
| Method | Returns | Description |
ClosestPoint(Vector3 position) | Vector3 | The point on (or in) the collider closest to a position. |
Raycast(Ray ray, out RaycastHit hit, float maxDistance) | bool | Raycast 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).
| Message | Parameter | Called when |
OnCollisionEnter | Collision | Started touching (solid). |
OnCollisionStay | Collision | Every physics step while touching. |
OnCollisionExit | Collision | Stopped touching. |
OnTriggerEnter | Collider | Something entered a trigger. |
OnTriggerStay | Collider | Every physics step while inside. |
OnTriggerExit | Collider | Something left a trigger. |
Useful members of Collision
| Member | Description |
collision.gameObject | The object we hit. |
collision.collider | The exact collider we hit. |
collision.relativeVelocity | How fast the two objects hit each other. |
collision.GetContact(0).point | Where they touched (world space). |
collision.GetContact(0).normal | The 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
- Each physics step, the engine checks which colliders overlap or touch.
- For solid collisions, it pushes Rigidbodies apart and adjusts their velocity (friction, bounce).
- It compares this step's contacts with the last step's: new pairs get
Enter, continuing pairs get Stay, ended pairs get Exit.
- Messages are sent to scripts on both objects. On a child collider of a Rigidbody, messages go to the Rigidbody's GameObject.
- 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
- No callback at all. Check: at least one object has a Rigidbody; both are 3D (or both 2D); the method signature is exactly right; the layers are allowed to collide in the Layer Collision Matrix.
OnTriggerEnter(Collision c): wrong parameter type. Triggers get a Collider.
- Scaling colliders to negative values or very thin shapes. Causes missed collisions. Use the collider's own Size/Radius fields.
See also
This page is a simplified, AI-assisted interpretation. For the complete and authoritative reference, see the official Unity documentation.