Teachers open the door but You must enter by yourself.

Open Media Lab.
オープンメディアラボ

弓のセットアップ

弓を Left Controller の子に配置

  1. Rotation の x を 90 に変更
  2. VRで確認して、弓の Position を調整する

アニメーション機能の設定(未設定の場合)

  1. Animation コンポーネントを追加
  2. アニメーションクリップ「Bow/BowPullAnimation」を追加
  3. アニメーションクリップ「Bow/BowReleaseAnimation」を追加
  4. play Automatically のチェックをはずす

ゲームコントローラの右トリガー入力を取得

  1. 弓の機能を呈するスクリプト Bow.cs を作成し、Bow にアタッチ
    
    using UnityEngine;
    using UnityEngine.InputSystem;
    
    public class Bow : MonoBehaviour
    {
    	public InputAction trigger;
    
    	void OnFire(InputAction.CallbackContext c)
    	{
    		//発射の処理を記述
    		Debug.Log("fire");
    	}
    
    	void Awake(){
    		trigger.performed+=OnFire;
    	}
    
    	void OnEnable(){
    		trigger.Enable();
    	}
    }
    
  2. トリガーボタンのInputAction を Inspector で設定
  3. トリガーボタンを引く動作が取得されていることをVRで確認する

弓を引く動作の実現

  1. Bow.csに以下のスクリプトを追記
    
    using UnityEngine;
    using UnityEngine.InputSystem;
    
    public class Bow : MonoBehaviour
    {
    	public InputAction trigger;
    	public Transform leftController;	
    	public Transform rightController;
    		
    	enum State{Idle, Grab, Pull}
    	State state=State.Idle;
    	
    	float Distance=>Vector3.Distance(leftController.position, rightController.position);
    
    	void Update()
    	{
    		switch(state){
    			case State.Idle:
    				if(Distance<0.4f){
    					state=State.Grab;
    				}
    				break;
    			case State.Grab:
    				if(Distance>0.4f){
    					GetComponent<Animation>().Play("BowPullAnimation");
    					state=State.Pull;
    				}
    				break;
    		}
    		// Debug.Log(Distance);
    	}
    
    	void OnFire(InputAction.CallbackContext c){
    		if(state==State.Pull){
    			//弦を離す処理を記述
    		}
    		Debug.Log("fire");
    	}
    
    	・・・
    }
    
  2. Left & Right Controller を Inspector で D&D
This site is powered by
Powered by MathJax