Physics2d.IgnoreLayerCollision not working when PlatformEffector2D is used
Solution 1:
The FixedUpdate
function is called every fixed framerate frame. When dropDown
is false, Physics2D.IgnoreLayerCollision(9, 8, false);
is called every fixed framerate frame. I do think this is the problem. You need to call this once only. Similar thing happens when dropDown
is true. Physics2D.IgnoreLayerCollision(9, 8, true);
is called every fixed framerate frame but this should only be called once.
Move that code directly to your getInput
function so that they are called once only. Ofcourse, you can use boolean variables in the FixedUpdate
function to fix this but that's totally unnecessary.
void getInput()
{
if(Input.GetKeyDown(KeyCode.S))
{
Physics2D.IgnoreLayerCollision(9, 8, true);
playerCollider.enabled = false;
platformCollider.enabled = false;
playerCollider.enabled = true;
platformCollider.enabled = true;
}
else if(Input.GetKeyUp(KeyCode.S))
{
Physics2D.IgnoreLayerCollision(9, 8, false);
}
}
getInput
must be called from the Update function. Finally, make sure that both objects are set to layer 8
and 9
in the Editor or code. It won't work if they are not set to these layers.
Using PlatformEffector2D
with Physics2D
:
With your new screenshot in your question, Physics2D.IgnoreLayerCollision
is not currently working because you are using PlatformEffector2D
on your colliders.
It doesn't work because PlatformEffector2D
have it's own collider mask settings which is enabled by default and overrides whatever you set to Physics2D.IgnoreLayerCollision
. You can use PlatformEffector.colliderMask
to select the layers or disable PlatformEffector2D
's mask so that you can use Physics2D.IgnoreLayerCollision
. I suggest disabling PlatformEffector2D
's mask as that's because it's more easy to use Physics2D.IgnoreLayerCollision
.
You can disable PlatformEffector2D
's mask by setting PlatformEffector2D.useColliderMask
to false
. This must be done for both the player and the platforms PlatformEffector2D
component.
Just add to the Start
function:
void Start()
{
PlatformEffector2D playerEffector = playerCollider.GetComponent<PlatformEffector2D>();
PlatformEffector2D platformEffector = platformCollider.GetComponent<PlatformEffector2D>();
//Disable PlatformEffector2D
playerEffector.useColliderMask = false;
platformEffector.useColliderMask = false;
}
Also modified your title to reflect that you are using PlatformEffector2D
.