OnTriggerEnter/Exit not working when changing parent of colliding object

Hey Unity devs, I was wondering if you could help me with an issue?

I have a ship that can land within a trigger “landing zone”, when it lands and leaves it works as expected, but I need to change the parent of the ship to the trigger box. The trigger box needs to move, because I want the smaller ship to be able to land in a bigger ship, and for the bigger ship to take the small ship along with it.

Everything works as I’d think UNTIL I change the parent, as soon as I swap the parent The OnTriggerEnter/Exit functions get called every frame and it stops working as expected.

It also has the knock on effect that the triggers inside the ship - for example the trigger near the seat to allow the player to take control Stops detecting when the player leaves the trigger.

Having done some research it looks like it’s something to do with compound colliders being made? Anyone know a way around this?

Video of issue:

What are the components on ShipParent?

I ran a quick test to refresh about compound colliders. In Unity Manual, it’s noted that

I don’t know your set up, so I’m just running a generic test with:

private void OnTriggerEnter(Collider other)
    {
        ObjToParent.gameObject.transform.parent = parentTo.transform;
        Debug.Log("Enter:" + other.gameObject.name);
    }
  1. Despite being compound colliders, CubeAParent, along with CubeAChild would both each receive individual callbacks when entering a trigger collider. (returns : CubeAParent, and CubeAChild).

But in your case, you’ve specified only to trigger for “LandingGear”.

  1. TriggerEnter is called again when, first call - rigidbody gameobject is independent and inside trigger collider, second call - rigidbody’s parent is changed.

  2. TriggerExit DOES NOT call any rigidbody child inside the triggercollider at the starting frame OR when parents changed.

Therefore,

  1. TriggerExit shouldn’t be called at all? So print out the name of the collider that is exiting to see what’s happening.

  2. I Noticed that in the second test the gameobject returned is : LandingGearColliders. But the first working test is, LandingGearsColliders (2). So if you have multiple objects tagged as “LandingGear”, then your code will get called multiple times.

Conclusion:
Before starting the frame or play → try having the StarShipAP OUTSIDE of the trigger collider before parenting it. OR be more specific to when you’re executing the code.
(Add extra if statement) (If (other.gameObject.TryGetComponent(out StarShip ship)))

I replicated the same issue by have 3 objects.
Trigger Collider - Parents CubeA to CubeB.
CubeA - compounded colliders with parent being the only object with a rigidbody.
CubeB - just a transform.

Issue only happens when CubeA starts the first frame inside Trigger Collider.
7422803--908438--Screenshot (307).png

Thanks for such an in-depth and detailed reply, you helped me understand a lot more about how this is all working now. I did manage to butcher something together with help from elsewhere which you can see here:
https://www.youtube.com/watch?v=Ye_asmbNfNw

Based on what you were saying though, each landing gear “foot” had a collider on it, instead, I have now created an empty “groundcheck” object, that is centred under the ship, this has its own small trigger collider on it and it has the tag of “landinggear” and it’s the only thing with that tag now. With some extra checks, it all seems to be working as intended!

1 Like

Glad it’s working as intended! Game is looking good

Thank you! It’s getting to a stage where I’m thinking of swapping out some of the programmer art with some better stuff now at least :slight_smile: