Unityで爆風を表現するのに以下の無料アセットが使える。
炎や煙などの見た目だけでなく、爆風によって周りのオブジェクトを吹き飛ばすことが出来るのだが、爆風の範囲内にあるRigidbodyを持つオブジェクトが全て吹き飛ばされてしまうので、レイヤーの指定により爆風の影響を受けるオブジェクトを限定できるように改造する。
使用バージョン:
Unity 4.5.1f3
Detonator Explosion Framework 1.04
//tweak the position such that the explosion center is related to the explosion's direction _explosionPosition = transform.position; //- Vector3.Normalize(MyDetonator().direction); // _colliders = Physics.OverlapSphere (_explosionPosition, radius); _colliders = Physics.OverlapSphere (_explosionPosition, radius, -1 - (1 << 4 | 1 << 8));
55行目:Physics.OverlapSphereの第3引数のレイヤーマスクを指定する。
レイヤーマスクの指定は若干複雑だが、
-1 : Everything
0〜 : Tags & Layersで指定した順番
をビット演算して指定する。
上のサンプルコードではEverythingから4番と8番のレイヤーを除外している。
実行速度を考慮するならば、Awake()などであらかじめ計算した値をメンバ変数に保持しておくと良い。