Conditions and prerequisites for Collision triggering:
- Condition: The Colliders of the two objects are in contact with each other and at least one of the objects has a Rigidbody component.
- Prerequisite: At least one object's rigid body component is in Dynamic Mode.
Trigger (trigger) triggered conditions and prerequisites:
- Condition: One object's collider (Collider) enters another object's trigger (Trigger) range.
- Prerequisite: At least one object's collider (Collider) and trigger (Trigger) are active.
the difference:
- Collision triggers can detect the collision of objects and produce actual physical reactions, such as the force generated during collisions, the movement of objects, etc.; while Trigger triggers are only used to detect the entry and exit of objects and do not produce actual physical reactions. .
- Collision triggers require at least one object to have a rigid body component, while Trigger triggers do not require objects to have a rigid body component.
Code example:
// Collision触发器的例子
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))
{
Debug.Log("Player collided with an object.");
// 在此处可以添加处理碰撞的代码逻辑
}
}
// Trigger触发器的例子
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Enemy"))
{
Debug.Log("Enemy entered the trigger.");
// 在此处可以添加处理触发器的代码逻辑
}
}