How can I jump down from a platform in Unity?
I use two 2D colliders: one is my character, another one is platform. These colliders allow my character to stand on platform. Now whenever a characters ducks I show a button, when it is clicked I want my character to go through platform down. How can I achieve this?
I researched a few options and none of them worked for me. Let me explain them.
1 I can make the character collider isTrigger = true;
. There is a serious problem with that option, because character would for a moment ignore all collissions. So, if for example, previously he would loose health due to bullet being hit into him, now the bullet will just go through him. Of course I can duplicate the code from the OnColliderEnter2D
to the OnTriggerEnter2D
. But it is a mess, so I feel that it would produce a technical debt. Hence, I want to avoid it.
2 I can make the platform collider isTrigger = true;
. The problem with this approach is that now anyone standing on the platform would fall from it. Not only the character who clicked the button to jump through the platform down.
3 I can disable the collision of the colliders layers. Here the issue is that I feel it is a wrong way to treat layers. They are not intended to be used like that. My feelings can be easily proven by an example. Let us suppose we have two enemies with the same layer standing on the same platform. One of the enemies wants to go down, so I disable the layer collision. And as a result two of the enemies go down. Yeah, I actually want a generic solution for the down jump. Such a solution which would be applicable not only as if it is the last feature I add into my game. I want to be able to empower enemies later on to do the same things as a character does.
So, I basically need something which would take the character collider and allow it to go down through the platform collider once.
Physics2D.IgnoreCollision
I linked above is more granular - it ignores just one pair of colliders, regardless of what layers they're on. \$\endgroup\$