Search Unity

box casting

Discussion in 'Documentation' started by NAVIDabc, Jul 7, 2022.

  1. NAVIDabc

    NAVIDabc

    Joined:
    Feb 20, 2021
    Posts:
    1
    hello im new in unity , i want to set 2 layer mask for boxcast but i dont know how , like i want to say if the player collider was touching ground and wall layer collider the player is grounded
    || and && didnt work in this case ,can anyone help?

    Code (CSharp):
    1.     private bool isGrounded()
    2.     {
    3.         RaycastHit2D raycast = Physics2D.BoxCast(collider.bounds.center, collider.bounds.size, 0, Vector2.down, 0.1f, groundLayer);
    4.         return raycast.collider != null;
    5.     }
     
  2. CharlesWard

    CharlesWard

    Unity Technologies

    Joined:
    Apr 19, 2017
    Posts:
    23
    Use the bitwise logic operators (single | and &) in this case, not the Boolean operators (double || and &&). The bitwise operators operate on each single digit in the binary number. They are often used with values that are treated as masks or flags.

    See https://docs.unity3d.com/2020.3/Documentation/Manual/layermask-add.html for an example of how to add an additional layer to a layer mask.