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. Main.csに以下のスクリプトを追記
  2. 
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.InputSystem;
    
    public class Main : MonoBehaviour
    {
    	void Awake(){
    		XRIDefaultInputActions inputActions=new XRIDefaultInputActions();
    		inputActions.Enable();
    		inputActions.XRIRightHandInteraction.Activate.performed+=OnActivate;
    	}
    
        void Start(){
    		GameObject.Find("/Horse").GetComponent<Horse>().Run();
    	}
    
    	void OnActivate(InputAction.CallbackContext context){
    		Debug.Log("pressed");
    	}
    }
    
  3. トリガーボタンを引く動作が取得されていることをVRで確認する

弓を引く動作の実現

  1. Main.csに以下のスクリプトを追記
    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.InputSystem;
    
    public class Main : MonoBehaviour
    {
    	void Awake(){
    		XRIDefaultInputActions inputActions=new XRIDefaultInputActions();
    		inputActions.Enable();
    		inputActions.XRIRightHandInteraction.Activate.performed+=OnActivate;
    	}
    
    	Transform leftHand,rightHand;
    
    	enum State{Idle, Pull, Ready}
    	State state=State.Idle;
    
        void Start(){
    		GameObject.Find("/Horse").GetComponent<Horse>().Run();
    
    		leftHand=GameObject.Find("Left Controller").transform;
    		rightHand=GameObject.Find("Right Controller").transform;
    
    	}
    
    	void Update()
    	{
    		float d=Vector3.Distance(leftHand.position, rightHand.position);
    		Debug.Log(d);
    		switch(state){
    			case State.Idle:
    				if(d<0.4f) state=State.Pull;
    				break;
    			case State.Pull:
    				if(d>0.4f){
    					GameObject.Find("Bow").GetComponent<Animation>().Play("BowPullAnimation");
    					state=State.Ready;
    				}
    				break;
    		}
    	}
    
    	void OnActivate(InputAction.CallbackContext context){
    		if(state==State.Ready){
    			GameObject.Find("Bow").GetComponent<Animation>().Play("BowReleaseAnimation");
    			state=State.Idle;
    		}
    	}
    }
    

This site is powered by Powered by MathJax