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?
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”.
TriggerEnter is called again when, first call - rigidbody gameobject is independent and inside trigger collider, second call - rigidbody’s parent is changed.
TriggerExit DOES NOT call any rigidbody child inside the triggercollider at the starting frame OR when parents changed.
Therefore,
TriggerExit shouldn’t be called at all? So print out the name of the collider that is exiting to see what’s happening.
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.
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!