Skip to main content
added 16 characters in body
Source Link
  • You can go to Edit->Project Setting->Physics or Physics 2D->Layer Collision Matrix. To change what you want your player pass through or not.
  • To change it via code like what you wanted, I have a solution here. Ex: your Player's Layer position is 6 & your Enemy is 7.

enter image description here

private bool isSwimmingUnderWater = false;
void Update()
{
    if (Input.GetKeyDown(KeyCode.A))
    {
        isSwimmingUnderWater = !isSwimmingUnderWater;
        Physics2D.IgnoreLayerCollision(6, 7, flagisSwimmingUnderWater); //it will set the val of the Layer Collision of the Player and Enemy whether true or false
    }
}

I have tested it with my game and it works.

  • You can go to Edit->Project Setting->Physics or Physics 2D->Layer Collision Matrix. To change what you want your player pass through or not.
  • To change it via code like what you wanted, I have a solution here. Ex: your Player's Layer position is 6 & your Enemy is 7.

enter image description here

private bool isSwimmingUnderWater = false;
void Update()
{
    if (Input.GetKeyDown(KeyCode.A))
    {
        isSwimmingUnderWater = !isSwimmingUnderWater;
        Physics2D.IgnoreLayerCollision(6, 7, flag); //it will set the val of the Layer Collision of the Player and Enemy whether true or false
    }
}

I have tested it with my game and it works.

  • You can go to Edit->Project Setting->Physics or Physics 2D->Layer Collision Matrix. To change what you want your player pass through or not.
  • To change it via code like what you wanted, I have a solution here. Ex: your Player's Layer position is 6 & your Enemy is 7.

enter image description here

private bool isSwimmingUnderWater = false;
void Update()
{
    if (Input.GetKeyDown(KeyCode.A))
    {
        isSwimmingUnderWater = !isSwimmingUnderWater;
        Physics2D.IgnoreLayerCollision(6, 7, isSwimmingUnderWater); //it will set the val of the Layer Collision of the Player and Enemy whether true or false
    }
}

I have tested it with my game and it works.

added 100 characters in body
Source Link
  • You can go to Edit->Project Setting->Physics or Physics 2D->Layer Collision Matrix. To change what you want your player pass through or not.
  • To change it via code like what you wanted, I have a solution here. Ex: your Player's Layer position is 6 & your Enemy is 7.

enter image description here

private bool flag=true;isSwimmingUnderWater = false;
void Update()
{
    if (Input.GetKeyDown(KeyCode.A))
    {
    flag    isSwimmingUnderWater = !flag;isSwimmingUnderWater;
        Physics2D.IgnoreLayerCollision(6, 7, flag); //it will set the val of the Layer Collision of the Player and Enemy whether true or false
    }
}

I have tested it with my game and it works.

  • You can go to Edit->Project Setting->Physics or Physics 2D->Layer Collision Matrix. To change what you want your player pass through or not.
  • To change it via code like what you wanted, I have a solution here. Ex: your Player's Layer position is 6 & your Enemy is 7.

enter image description here

bool flag=true;
if (Input.GetKeyDown(KeyCode.A))
{
    flag = !flag;
    Physics2D.IgnoreLayerCollision(6, 7, flag); //it will set the val of the Layer Collision of the Player and Enemy whether true or false
}

I have tested it with my game and it works.

  • You can go to Edit->Project Setting->Physics or Physics 2D->Layer Collision Matrix. To change what you want your player pass through or not.
  • To change it via code like what you wanted, I have a solution here. Ex: your Player's Layer position is 6 & your Enemy is 7.

enter image description here

private bool isSwimmingUnderWater = false;
void Update()
{
    if (Input.GetKeyDown(KeyCode.A))
    {
        isSwimmingUnderWater = !isSwimmingUnderWater;
        Physics2D.IgnoreLayerCollision(6, 7, flag); //it will set the val of the Layer Collision of the Player and Enemy whether true or false
    }
}

I have tested it with my game and it works.

deleted 1 character in body
Source Link
  • You can go to Edit->Project Setting->Physics or Physics 2D->Layer Collision Matrix. To change what you want your player pass through or not.
  • To change it via code like what you wanted, I have a solution here. Ex: your Player isPlayer's Layer position is 6 & your Enemy is 7.

enter image description here

bool flag=true;
if (Input.GetKeyDown(KeyCode.A))
{
    flag = !flag;
    Physics2D.IgnoreLayerCollision(6, 7, flag); //it will set the val of the Layer Collision of the Player and Enemy whether true ofor false
}

I have tested it with my game and it works.

  • You can go to Edit->Project Setting->Physics or Physics 2D->Layer Collision Matrix. To change what you want your player pass through or not.
  • To change it via code like what you wanted, I have a solution here. Ex: your Player is Layer position is 6 & your Enemy is 7.

enter image description here

bool flag=true;
if (Input.GetKeyDown(KeyCode.A))
{
    flag = !flag;
    Physics2D.IgnoreLayerCollision(6, 7, flag); //it will set the val of the Layer Collision of the Player and Enemy whether true of false
}

I have tested it with my game and it works.

  • You can go to Edit->Project Setting->Physics or Physics 2D->Layer Collision Matrix. To change what you want your player pass through or not.
  • To change it via code like what you wanted, I have a solution here. Ex: your Player's Layer position is 6 & your Enemy is 7.

enter image description here

bool flag=true;
if (Input.GetKeyDown(KeyCode.A))
{
    flag = !flag;
    Physics2D.IgnoreLayerCollision(6, 7, flag); //it will set the val of the Layer Collision of the Player and Enemy whether true or false
}

I have tested it with my game and it works.

Source Link
Loading