Unity2021から標準パッケージとなった”Visual Scripting(ビジュアルスクリプティング)”(旧Bolt)を使用して、プレイヤー(”Player”タグが付いたゲームオブジェクト)に当たると消えるゲームオブジェクトの作り方を紹介します。
プレイヤーとしては以下を使います。
完成動作
プレイヤー(unitychan)がCylinder(赤)に当たってから2秒後に消えます。
作り方
1.シーンに各オブジェクトを配置
1-1.以下のように配置する。床をPlane、プレイヤーをunitychan、当たったら消えるゲームオブジェクトをCylinder(赤)とした。

2.プレイヤー(”Player”タグが付いたゲームオブジェクト)に当たったら消えるビジュアルスクリプトを作成
2-1.Cylinderに”Script Machine”コンポーネントを追加


2-2.”Graph”プロパティの”New”ボタンを押下

2-3.ファイルの保存ダイアログが開かれるので任意の場所にHitPlayerDestroyという名前で保存する。

2-4.”Edit Graph”ボタンを押下して、以下のようにグラフを作成する。
※DestroyユニットのTの値で当たってから何秒後に消えるかを指定できる。

3.Colliderの”Is Trigger”にチェックを入れない
3-1.消したいブロック(Cylinder)のCapsule Colliderの”Is Trigger”にチェックを入れない。
※ここにチェックが入っているとOnCollisionEnterイベントではなく、OnTriggerEnterイベントが発火してしまう。デフォルトではチェックは入っていないので大丈夫。

4.プレイヤーオブジェクトのTagに”Player”を設定
4-1.プレイヤーオブジェクト(今回はunitychan)のTagに”Player”を設定

完成
プレイヤー(unitychan)がCylinder(赤)に当たってから2秒後に消えます。
コメント