2
\$\begingroup\$

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.

\$\endgroup\$
7
  • \$\begingroup\$ Did you look at Physics2D.IgnoreCollision for this purpose? \$\endgroup\$
    – DMGregory
    Feb 9, 2021 at 18:22
  • \$\begingroup\$ @DMGregory, yes, I did, thanks. That is 3. I want a fine grained control here. I think that collision layers matrix should always be invariant. Otherwise the game will be hard to maintain and extend. \$\endgroup\$
    – some1 here
    Feb 9, 2021 at 18:22
  • 2
    \$\begingroup\$ It sounds like you're describing Physics2D.IgnoreLayerCollision. The Physics2D.IgnoreCollision I linked above is more granular - it ignores just one pair of colliders, regardless of what layers they're on. \$\endgroup\$
    – DMGregory
    Feb 9, 2021 at 18:25
  • \$\begingroup\$ @DMGregory, omg. It feels as if you are my savior. I will try it tomorrow ang get back to you with the results. But I am almost certainly sure that is what I was looking for. \$\endgroup\$
    – some1 here
    Feb 9, 2021 at 18:26
  • 2
    \$\begingroup\$ My superpower is reading the docs. 😉 \$\endgroup\$
    – DMGregory
    Feb 9, 2021 at 18:28

1 Answer 1

0
\$\begingroup\$

The @DMGregory's comment is what I was looking for.

I just take the character colliders and run for each one of them the call:

foreach(var characterPartCollider in characterPartsColliders) 
{
    Physics2D.IgnoreCollision(groundCollider, characterPartCollider);
}

This makes the character fall down through a platform. Then when I detect a collision with another platform I run the following piece of code to enable the collision with the previous platform back:

foreach(var characterPartCollider in characterPartsColliders) 
{
    Physics2D.IgnoreCollision(groundCollider, characterPartCollider, false);
}

Previously I confused the solution with the layer collision disabling. This solution is better, because it is more fine frained - with the code above we affect only what we intend to affect - that is the relationship between the character collider and a platform collider and absolutely nothing else.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .