I have two players in my 2d unity game both moving independently but when one jumps on the others head it should stay there till it jumps off. I managed to get it to stick using
You can just unparent it by settings the parent to whatever needs to be the parent. If there’s no object you want to parent it to, setting a parent to “null” will effectively make it a top-level object in the world (which will be represented as top-level object in the hierarchy). If there is a parent, for instance your own “world parent”, set that one as the parent.
The mechanism you can use - based on your current logic - is “OnCollisionExit”. However, I would recommend checking whether or not you should (un-)parent the character in the first place, because these collision messages are raised for all collisions that occur and you probably don’t want to parent the object to everything it collides with. You might also need additional logic to detect that you’re actually on top of another character, unless you have a special collider setup.
if the cat need to jump off, the question is does the jump mechanic good enough to trigger OnCollisionExit2D
i recommend to write a debug.Log"Cat is jumped and it leave the collider" in side OnCollisionExit2D, just a simple up jump and land on the head and see what the console will say
You can just set it to null if you don’t have any special parent you want to reset it back to:
collision.gameObject.transform.parent = null;
There’s also SetParent, which you can use if you need more control about how the transform values are affected by the change.
Though as I said, it depends what you want to parent it to. Null will unparent it completely so that it the object becomes a root object itself.
Small side note: Use CompareTag rather than the equality operator == when you want to compare tags. It doesn’t make a difference in how things work, but it’s a small improvement on the technical side.
collision.gameObject.transform.parent = null;
fixed it so thank you both for commenting it is very much appreciated, will definately be back again at somepoint lol thanks again