What are the conditions for Collision (collision) and Trigger (trigger) to trigger?

Conditions and prerequisites for Collision triggering:

  1. Condition: The Colliders of the two objects are in contact with each other and at least one of the objects has a Rigidbody component.
  2. Prerequisite: At least one object's rigid body component is in Dynamic Mode.

Trigger (trigger) triggered conditions and prerequisites:

  1. Condition: One object's collider (Collider) enters another object's trigger (Trigger) range.
  2. Prerequisite: At least one object's collider (Collider) and trigger (Trigger) are active.

the difference:

  1. 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. .
  2. 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.");
        // 在此处可以添加处理触发器的代码逻辑
    }
}

Guess you like

Origin blog.csdn.net/qq_20179331/article/details/132340055