- Home /
Trigger Colliders: adding/removing rigidbody/collider at runtime is unreliable or confusing
I have a simple script:
public class TrackingTrigger : MonoBehaviour
{
public List<Transform> Inside = new List<Transform>();
void OnTriggerEnter(Collider other)
{
Debug.Log("Enter: ("+this+", "+ other+")");
Inside.Add(other.transform);
}
void OnTriggerExit(Collider other)
{
Debug.Log("Exit: ("+this+", " + other+")");
Inside.Remove(other.transform);
}
}
I Create the scene as follows:
P: Parent GameObject
A: Child GameObject Under parent P
B: Child GameObject Under parent P
I add TrackingTrigger, SphereCollider, RigidBody in all three (P,A,B) and hit play. It works as expected: each Trigger has inside it all that collide.
1) Test Case: Non Symmetry
a) Remove B.SphereCollider : OnTriggerExit is not called at P,A,B
b) Add B.SphereCollider : OnTriggerEnter is called
Problem: 1a => not symmetric behaviour => Objects are "double added" in lists
2) Test Case: Mindblow
a) Remove B.RigidBody: causes OnEnter: (A,B) (P,A) (B,A)
Problem1: "Exit" is never called, instead "Enter" is called
Problem2: Since B has no rigidbody it attempts to use his parent P rigidbody, but why is A affected ? ie (P,A) makes no sense, it doesn't even cause (A,P) symmetrical callback.
b) Add B.RigidBody: causes Exit: (A,B) (B,A) Enter: (A,B) (B,A) (P,B) (B,P)
Final Lists:
P={B,A,A,B}
A={P,B,B}
B={P,A,A,P}
3) Another issue i had with trigger colliders is that in unity every trigger, checks for all nearby colliders, there is no way to filter "triggers" by layer (without lowering performance). So i cannot use "Collision Layer Matrix" optimizations.
4) To me all this seems confusing. I am testing the system to its limits to understand how the api "works". I was thinking of using this for a system where big triggers get "refined" into smaller triggers when something gets inside them for ultra detailed physics. I don't want to monopolize your time, only answer if it doesn't take much time, I can figure things on my own, if you point me to the right direction. thank you
wow, very good post. Order, everything explained.... but... I have no *** idea how can i help you :( (or too complex or too lazy)
Hey there,
i was raised in Unity with the knowledge never to parent Rigidbody together as seen for example in this Post. Aparently that is not valid anymore. Good to know :D o far intresting Problem. I think i'll try to reproduce that myself. Did you check if it is related to the parenting or does the same issue occure if you undo all or parts of the parenting? I'm asking since im currently facing a somewhat similar weird behaviour which utilizes pretty much exactly the same script that you use. I posted it here.
Entry is a bit frustrating for just this application. I tossed it and started using OnTriggerStay with a boolean to only check once to make sure that an Enter trigger is actually valid.
Rigidbodies treats colliders as a whole. When you remove B's Rigidbody, B collider joins to P Rigidbody, which updates that Rigidbody, that's why OnEnter is being called.
Using rigidbodies in parents and childrens is delicated, though, it's not forbidden. Ragdolls uses lots of rigidbodies trough all it's hierarchy as an example
Answer by xarismax · May 12, 2018 at 04:58 PM
thanks for the answer. Solution given by Captain_Pineapple Comment.
"you cannot have rigid body on both parent and child".
Your answer
Follow this Question
Related Questions
Does the "Layer Collision Matrix" affect triggers? 1 Answer
How to achieve this basic physics effect? Detect when to change layers 2 Answers
Wind physics script 1 Answer
If trigger hit, spawn it 1 Answer
Collission and Trigger on Same object 0 Answers