Physics2D.Linecast explanations

Hello,

in the 2d platformer tutorial, in the ‘hero’ script there is this line (to test if the hero is grounded):

grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));  

there is 2 things i don’t understand:

  • Physics2D.Linecast, according to the manual, return a RaycastHit2D object, but grounded is a boolean object !!!
  • why must i use a ‘Bitwise Left Shift Operator’ in the third parameters, instead of LayerMask.GetMask(“Ground”) ???

if anyone can explain.

thanks.

Physics2D.Linecast, according to the
manual, return a RaycastHit2D object,
but grounded is a boolean object !!!

RaycastHit2D has its bool operator overloaded, so it can be read as a bool. If you don’t know what this means, Google “c# operator overloading”.

why must i use a ‘Bitwise Left Shift
Operator’ in the third parameters,
instead of LayerMask.GetMask(“Ground”)

I think LayerMask.GetMask() didn’t exist in earlier Unity versions, so you had to convert the layer number to a layermask yourself.