Teachers open the door but You must enter by yourself.
using UnityEngine;
using UnityEngine.InputSystem;
public class Bow : MonoBehaviour
{
public InputAction trigger;
void OnFire(InputAction.CallbackContext c){
Debug.Log("fire"); //コンソールウィンドウにFireと出力
}
void Awake(){ //トリガーが押されたらOnFire関数を実行するように設定
trigger.performed+=OnFire;
}
void OnEnable(){
trigger.Enable();
}
}
using UnityEngine;
using UnityEngine.InputSystem;
public class Bow : MonoBehaviour
{
public InputAction trigger;
public Transform leftController;
public Transform rightController;
//コントローラ間の距離を取得するgetter
float Distance=>Vector3.Distance(leftController.position, rightController.position);
void Update()
{
Debug.Log(Distance);
}
・・・(以下、省略)
}
using UnityEngine;
using UnityEngine.InputSystem;
public class Bow : MonoBehaviour
{
public InputAction trigger;
public Transform leftController;
public Transform rightController;
float Distance=>Vector3.Distance(leftController.position, rightController.position);
enum State{Idle, Grab, Pull} //何もしていない・弦を握った・弦を引いた、の3状態
State state=State.Idle;
void Update()
{
switch(state){
case State.Idle:
if(Distance<0.4f){ //コントローラ距離が40cmより小さい
state=State.Grab;
}
break;
case State.Grab:
if(Distance>0.4f){ //コントローラ距離が40cmより大きい
GetComponent<Animation>().Play("BowPullAnimation"); //弓を弾くアニメーション
state=State.Pull;
}
break;
}
// Debug.Log(Distance);
}
void OnFire(InputAction.CallbackContext c){
if(state==State.Pull){
//弦を離すアニメーションを実行
//状態をIdleに移行
}
// Debug.Log("fire");
}
・・・(以下、省略)
}