4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unity - LayerMaskの中に対象のLayerが含まれているかチェックする方法

Posted at

概要

Raycastでヒットした特定のレイヤーを判別して処理を分ける、みたいな使い方ができます。

実装

// LayerMaskに含まれていた場合
if (((1 << gameObject.layer) & layerMask) != 0;) {
	Debug.Log ("含まれている");
}

// LayerMaskに含まれていない場合
if(((1 << gameObject.layer) & layerMask) == 0) {
	Debug.Log ("含まれていない");
}

メソッドにしておくと便利。

// LayerMaskに対象のLayerが含まれているかチェックする
private bool CompareLayer (LayerMask layerMask, int layer) {
	return ((1 << layer) & layerMask) != 0;
}

以上。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?