Question about Physics2D.Linecast

Hello Everybody,

iam using some code from the Example 2D Plattformer Project to handle my PlayerCollision - for checking if iam on the ground or hit an enemy.

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

My Question about that is: Why i cant use

instead of
“1 << LayerMask.NameToLayer(“Enemy”)”.

What is the difference about “1 <<” ? I do not know what this operator doing here exactly.

Would be nice if somebody could explain me that.

best Regards

LayerMask.NameToLayer(“Enemy”) gives you an integer that represents the index of that layer, but function Physics2D.Linecast doesn’t want the number of that particular layer. Instead it wants a layerMask, which represents a filter for all the layers.

The “1<<” part transforms the index of the layer (the LayerMask.NameToLayer) into a mask in the form of “00001000” in which the 1 is your “Enemy” layer (the position depending on the index of the layer) and the 0s are the rest of the layers. The linecast will interact with the layer marked as 1 and ignore the layers marked as 0 in the mask. Read the comment on the use of layers for further details.