Unity solves the problem that the parent node has a Rigidbody child node collision affecting the parent node

When I was working on a project recently, I found that the parent node has Rigidbody and Collider, and the child node has Collider, which will trigger two collisions, and the triggered gameobject.name is the parent node. I encountered it before, but I didn't pay attention to this problem.
Both parent and child nodes are hung with test code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Test
{
    public class TriggerTest : MonoBehaviour
    {
        private void Start()
        {
            
        }
        // Start is called before the first frame update
        private void OnTriggerEnter(Collider other)
        {
            Debug.Log(gameObject.name);
        }
    }
}

Create two objects, parent object Cube, child object Sphere, Collider hook Trigger.
The first case : Rigidbody on Cube, Collider on Shpere, Collider on Shpere, and the test scripts on both objects are enable=true

 At this time, collide, only let the ball hit the object, and the print result is

If you delete the test script on the ball, use the ball to collide. turn out:

 

Test results (the above picture is just one type, the rest can be tested by yourself):
(1) There are Corllider and Rigidbody on the parent node, and Collider on the child object.
a. The parent node has the OnTriggerEnter interface, and the child node has the OnTriggerEnter interface. When it encounters the child object, it triggers twice, and prints their names. When it encounters the parent object, it triggers once and prints its own name.
b. The parent node has the OnTriggerEnter interface, and the child node does not have the OnTriggerEnter interface. When a child object is encountered, it is triggered once, and the name of the parent node is printed. Touch the parent object, trigger once, print the name of the parent node.
c. The parent node does not have the OnTriggerEnter interface, and the child node has the OnTriggerEnter interface. Touch the child object, print the name of the child object, touch the parent object, no response.
(2) There is a Corllider on the parent node, a Collider on the child object, and a Rigidbody on the child.
a. The parent node has or does not have the OnTriggerEnter interface, the child node has the OnTriggerEnter interface, touch the child node, the child node responds, and the parent node does not respond.
b. The parent node has the OnTriggerEnter interface, and the child node does not have the OnTriggerEnter interface, so there is no response.
(3) The parent node has Corllider and Rigidbody, and the child object has Collider and Rigidbody.
a. Touch the child node and the parent node respectively, which is equivalent to the collision of two separate objects. If the test script is mounted, there will be a response. If it is not mounted, there will be no response.
Summary:
1. If both the parent node and the child node have Rigidbody, it is equivalent to two independent objects, each touching their own.
2. If the parent node has a Rigidbody, but the child node does not, the child node is equivalent to a part of the parent node, it depends on who has a response interface, the parent node has a response interface, the parent node responds, the child node has an interface, and the child node has a response interface. Response, both have interfaces, the parent node will not affect the child node, but the child node response will make the parent node respond.
3. If the parent node does not have a Rigidbody, but the child node does, the child node will respond, but the parent node will not respond.
 

 

Guess you like

Origin blog.csdn.net/cuijiahao/article/details/122743189