Example scripts for collider events
Create and configure a trigger collider
A trigger colliderAn invisible shape that is used to handle physical collisions for an object. A collider doesn’t need to be exactly the same shape as the object’s mesh - a rough approximation is often more efficient and indistinguishable in gameplay. More info
See in Glossary does not collide with other colliders; instead, other colliders pass through it.
To create a trigger collider:
- Create a GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary:
- To make an invisible trigger collider, create an empty GameObject. In most cases, trigger colliders are invisible.
- To make a visible trigger collider, create a GameObject that has a meshThe main graphics primitive of Unity. Meshes make up a large part of your 3D worlds. Unity supports triangulated or Quadrangulated polygon meshes. Nurbs, Nurms, Subdiv surfaces must be converted to polygons. More info
See in Glossary. You should only create a visible trigger collider if it is okay for other GameObjects to visibly pass through it at run time.
- Add a Collider to the GameObject.
- Make the collider a trigger:
- To do this in the Editor, navigate to the collider’s InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info
See in Glossary and enable the Is Trigger property.
- To do this via script, set the collider’s
IsTrigger
to true
.
Configure trigger collisions
Make sure there is at least one dynamic collider in the collisionA collision occurs when the physics engine detects that the colliders of two GameObjects make contact or overlap, when at least one has a Rigidbody component and is in motion. More info
See in Glossary. At least one GameObject involved in a trigger collision must have a physics body (a Rigidbody or an ArticulationBody). In most cases, trigger colliders are stationary and static (that is, they have no physics body), and the colliders that pass through them are moving and dynamic (that is, they have a physics body).
Experiment with the size and shape of your trigger collider. For gameplay and simulation, triggers might need some adjustment to make them feel intuitive for the player. For example, you could experiment with making a trigger collider slightly larger than its associated visible GameObject, so that it has a wider radius.
Example scripts for collider events
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。