I am participating in the nuggets Community game creative submission Contest. For details, please see: Game Creative Submission Contest
Collider: OnCollisionEnter
Trigger: OnTriggerEnter
Conditions: At least one object must have a Rigidbody component and both objects must have a Collider component
Experiment 1 OnCollisionEnter and OnTriggerEnter trigger conditions
Create Cube and Sphere in the scene, add Rigidbody and Collider components, And mount the script to listen for “void OnCollisionEnter(Collision Collision)” and “void OnTriggerEnter(Collider Other)”
public class Test : MonoBehaviour { void OnCollisionEnter(Collision collision) { Debug.Log(gameObject.name + " - OnCollisionEnter - " + collision.gameObject.name); } void OnTriggerEnter(Collider other) { Debug.Log(gameObject.name + " - OnTriggerEnter - " + other.gameObject.name); }}Copy the code
-
The IsTrigger option is unchecked for both Collider components. Both components are colliders and the mounted script will trigger the OnCollisionEnter function.
-
One with IsTrigger checked, one unchecked, one as collider, and one as trigger, all mounted scripts fire OnTriggerEnter, and none of them fire OnCollisionEnter.
-
The IsTrigger option is selected for both of them, and both will act as triggers. The mounted script will trigger “OnTriggerEnter” and neither will trigger “OnCollisionEnter”.
OnCollisionEnter; OnTriggerEnter
In some cases, the child’s collider/trigger will trigger the parent’s “OnCollisionEnter”, “OnTriggerEnter” functions:
-
For example, if the parent object adds Rigidbody and has no collider, and the child object does not have Rigidbody and has collider, the parent object will trigger OnCollisionEnter, and the child object will not.
-
If the parent object adds Rigidbody and has collider, and the child object does not have Rigidbody and has collider, the parent object will trigger OnCollisionEnter, and the child object will still not.
-
The above test OnTriggerEnter has the same result as OnCollisionEnter
So: the object with Rigidbody component will execute “OnCollisionEnter” when its collider or its child collider collides; OnTriggerEnter is executed when the trigger itself or its child collides.
However, adding the RigidBody component to the parent object and the child object respectively will detect and call the corresponding function when the collider and trigger are executed. The child object only fires the child object callback, and the parent object only fires the parent object callback.
If the parent object wants to mask the child collider/trigger, add Rigidbody to the child object.
If both objects have a Collider component and at least one object or its parent has a Rigidbody component, a collision can occur.
OnCollisionEnter, OnTriggerEnter
Tests: Object A, parent object B, child object C
- Add colliders to A, B and C, and Rigidbody to A and B.
In A’s “OnCollisionEnter(Collision Collision)” callback, whether A hits B or C, Collision. The gameObject is B (i.e., a Rigidbody objects), collision. Starts. The gameObject is B or C (namely have collided box).
- A, B, C all add trigger, A, B add Rigidbody.
In A’s “OnTriggerEnter(Collider Other)” callback, collide B, other.gameObject == B; Hit C, other.gameObject == C. (i.e. objects with triggers)
Added a:
After the two colliders/triggers have intersected, turn off either GameObject.Activeself or Collider.enabled and OnTriggerExit will not fire; OnTriggerEnter fires when it is turned on again.