- Home /
Why is OnCollisionEnter2D called when it's layer's collisions are being ignored?
I'm sure this is down to my lack of understanding of the unity physics system.
I'm trying to implement a simple one-way platform for a 2d game (based on character y velocity) but have got stuck on creating a reliable way to let the character 'drop down' from a platform on pressing the down arrow.
While trying to debug and whittle down the code, I've found myself with this strange debug script:
using UnityEngine;
using System.Collections;
public class DebugPlatform : MonoBehaviour
{
void OnCollisionEnter2D(Collision2D other)
{
if(Physics2D.GetIgnoreLayerCollision (other.collider.gameObject.layer, this.gameObject.layer))
print ("ERROR: How does this happen?!!");
else
print("This is normal.");
}
}
When I place this on my one way platform and mess about in game, sometimes the character refuses to drop down on command, and the error message is printed.
From what I understood, GetIgnoreLayerCollision returned whether the collisions between the layers were being ignored or not.
So my questions are:
Why is OnCollisionEnter2D being called even though the layers are supposed to be ignored?
Is this ignore value just a request, not absolute? Maybe some collision optimization that I have to override?
I can't imagine this is some multi-threading issue, as the error appears regularly, after I do a normal jump on the platform.
Am I doing something completely wrong?
From what I can see, this really, really looks like a bug. Unless someone spots something I can't, you should probably post a bug report.
@Baste Noooooo... I had high hopes for IgnoreLayerCollision. I was sure I was just doing something stupid. In the meantime I've figured out another way to implement this, just requires an extra platform specific collider.
@mallardz Your code appears to be good. This is most likely a Unity error as Baste suggested. Please file a bug report with clear reproducible steps and a repro project. Thanks.
Answer by b-cancel · Feb 20, 2017 at 09:42 AM
Here I am hoping that someone told you about the platform effector 2D and that you didn't waste time coding this flawlessly... or... since this is from 2014 that it existed...
My platform currently is a little different that the regular platform unity has created for us... but i simply applied my modifications on the platform effector 2D...
For those who wandered here now 3 years later... THIS BUG HAS BEEN FIXED...
This is exactly the code that I am using to allow drop downs from the platform that the standard platform effector 2D creates