Teachers open the door but You must enter by yourself.

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

矢のセットアップ

  1. 矢にRigidbodyコンポーネントを追加し、Use Gravity のチェックを外す
  2. Capsule Collider を追加し、Radius=0.03、Height=0.8、Is Trigger にチェックを付ける
  3. 矢の機能を呈するスクリプト Arrow.cs を作成し、Arrow にアタッチ
    
    using UnityEngine;
    
    public class Arrow : MonoBehaviour
    {
    	public float groundSpeed=7.5f;//馬の速度
    	public float launchSpeed=10f;
    	public float magnify=2f;
    
    	public GameObject bow;
    
    	bool flying=false;
    
    	float launched;    
    	public void Ready(){
    		transform.parent=bow.transform;
    		transform.localPosition=new Vector3(0, 0, -0.3f);
    		transform.localRotation=Quaternion.Euler(90f, 0f, 0f);
    		transform.localScale=new Vector3(1f, 1f, 1f);
    		GetComponent<Rigidbody>().linearVelocity=Vector3.zero;
    		flying=false;
    	}
    
    	public void Fire(){
    		var v=Vector3.forward*groundSpeed + transform.up*launchSpeed;
    		GetComponent<Rigidbody>().linearVelocity=v;
    
    		transform.parent=null;
    		launched=Time.time;
    		flying=true;
    	}
    
    	void Update()
    	{
    		if(flying){
    			var flyingTime=Time.time-launched;
    			if(flyingTime>3f){
    				Ready();
    			}else{
    				var scale = 1f + magnify*flyingTime;
    				transform.localScale=new Vector3(scale, scale, scale);
    			}
    		}
    	}
    }
    
  4. Bow を Inspector で D&D
  5. Bow.cs に以下のスクリプトを追記
    
    using UnityEngine;
    using UnityEngine.InputSystem;
    
    public class Bow : MonoBehaviour
    {
    	public GameObject leftController;	
    	public GameObject rightController;
    	public GameObject arrow;
    		
    	enum State{Idle, Pull, Ready}
    	public State state;
    	
    	void Start()
    	{
    		var a=new InputSystem_Actions();
    		a.Enable();
    		a.Player.Sprint.performed+=OnFire;
    
    		state=State.Idle;
    	}
    
    	void Update()
    	{
    		float d=Vector3.Distance(
    			leftController.transform.position,
    			rightController.transform.position
    		);
    		//Debug.Log(d);
    
    		switch(state){
    			case State.Idle:
    				if(d<0.4f) state=State.Pull;
    				break;
    			case State.Pull:
    				if(d>0.4f){
    					GetComponent<Animation>().Play("BowPullAnimation");
    					arrow.GetComponent<Arrow>().Ready();
    					state=State.Ready;
    				}
    				break;
    		}
    	}
    	void OnFire(InputAction.CallbackContext c){
    		Debug.Log("fire");
    		if(state==State.Ready){
    			GetComponent<Animation>().Play("BowReleaseAnimation");
    			arrow.GetComponent<Arrow>().Fire();
    			state=State.Idle;
    		}
    	}
    }
    
  6. Arrow を Inspector で D&D
  7. This site is powered by
    Powered by MathJax