Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Work-around for weird physics quirk when using Physics2D.IgnoreLayerCollision

Discussion in 'Physics' started by Nikovich, May 6, 2015.

  1. Nikovich

    Nikovich

    Joined:
    Jul 10, 2010
    Posts:
    69
    I'm working on a platformer and I'm trying to create the ability to dynamically place pitfalls in any landscape. Because these need to be able to work on any platform, I'm attempting to achieve this by creating hitboxes on the ground that temporarily turn off physics between the character layer and the ground layer; I can't directly affect the platform/ground hitboxes themselves.

    I'm using Physics2D.IgnoreLayerCollision(17,18,true) for this, it works as expected. When the player enters that htibox, the layers are turned off; when the player exits the hitbox, they're turned back on. There is a strange issue with the execution, however. If the player is currently colliding with the ground and these layers are turned off (when he hits the fall box), as long as they continue to face the same direction, they will float. They can stop and start moving as they please, but they will only fall if they change direction. Also, the pit will work as intended if they fall into it (so they are not currently colliding with Ground when the layers are turned off.)

    This is baffling me a bit and I was wondering if anyone else found a good workaround for this. I've tried forcing the character into a mid-air state, but as the character's platforming is controlled via a collider, he simply continues floating forward, just now as if he were in mid-jump. I have also attempted applying some vertical force to the rigidbody, both in upward and downward directions (through forcemod impulse), but he continues to float. I don't think my controller is causing his continual floating because he should always have gravity and is never set to isKinematic.

    Does this sound like a weird physics bug anyone has experienced before, or is the answer possibly somewhere deeper in my character controller? It's fairly simple and I experience the same issue if I manually turn the collision off in the matrix, however, he WILL fall if I deactivate the gameobject he is standing on.

    Edit: I just did some more testing, and I believe it has to be a physics quirk. I created a physics object with no extraneous controllers, just a collider and rigidbody, and replicated the same behavior when turning off the collider after it was colliding with the ground.
     
    Last edited: May 6, 2015