user3375189
user3375189

Reputation: 21

Unity3D: Recalculating collisions after Physics2D.IgnoreLayerCollision()?

I have my "player" gameObject standing atop a platform. He press a combination of keys, I verify that he's standing on the right kind of platform, and I run this code:

Physics2D.IgnoreLayerCollision(playerLayer, plataformLayer);

The intention being that he'd drop down from the platform. Instead, nothing happen immediately. The player is still atop the platform and can move around - only if he gets out of it and tries to get back to it, only at that point will he fall down from it.

So I made a temporary ugly workaround:

Physics2D.IgnoreLayerCollision(playerLayer, plataformLayer);
rigidbody2D.AddForce(transform.up * jump / 1.5f);

This will force the player to jump, making Unity "recalculate" the collisions and the player will, as I want to, pass through the platform. The even weirder part is: if the jump isn't high enough (100 force at 1 mass with 0.5 gravity scale seems to be the minimum), the player will still land on the platform, even though Unity is supposed to be ignoring those collisions.

I also tried pushing the player downwards, both with force and direct velocity, but no luck - he still collides with the platform, he only stops colliding with it after being away from it once.

Also, my player Rigidbody2D Detection Mode is set to Continuous, and I tried setting the platform up in many different ways, with and without a Rigidbody2D.

Any ideas on how to make the player instantly fall down from the platform as soon as collisions start being ignored? Thanks in advance.

Upvotes: 2

Views: 1164

Answers (3)

Gabe
Gabe

Reputation: 1812

One easy way to do this is after calling

Physics2D.IgnoreLayerCollision(playerLayer, plataformLayer);

You reset the player's layer:

player.gameObject.layer = playerLayer;

Upvotes: 1

Cimarolo
Cimarolo

Reputation: 11

I had been having the exact same issue. I have an alternative solution that you may find to your liking.

Rather than disabling or moving the terrain to be ignored, temporarily set the rigidbody2D of your player gameObject to kinematic. This will not cause you to 'drop' through the platform as you would like - but it will allow you to create the illusion that you are, by briefly using transform.Translate (or Vector3.Lerp, if you like) to move your character down via code. This will disable Rigidbody2D.AddForce, but will still allow for set movement via rigibdody2D.velocity, if that is preferable to manipulating the transform directly.

Ensure the downward velocity lasts only as long as you are Kinematic, and try to let it match the gravity of your game, and it looks pretty seamless. If you turn the rigidbody back on too quickly, it can (even with ignoreLayers :/ ) still jump back up to the platform - but if you leave it on too long, you can drop through more than one intended one-way platform, or even :O through the bottom of the stage! This is easily avoided if you can be careful - personally I used both MonoBehaviour.Invoke to set a very short time limit on how long the player would remain Kinematic (about 0.5 seconds), AND I disabled Kinematic as soon as they moved 0.5 units from the point where the 'hopDown' was initiated. If your gravity / platforms / character scale are vastly different than my own, you may have to experiment with some different numbers.

This has been my first attempt at an answer on stackoverflow, so I hope it has been helpful!

Upvotes: 1

GDM
GDM

Reputation: 90

It's sounds like you have covered a ton of possible solutions to this problem before coming here so, good job there. I listed a few alternative ideas below. I hope they help. Good luck!

  • Remove the collision object from the platform itself.
  • Offset the collision object to be in some remote location so it will not interact with the player. Offsetting the object may cause the collision to have a similar reaction to the jump you mentioned.

Upvotes: 0

Related Questions