unity3d 拋物線

只做備忘,代碼很亂


生成一個空物體,加上TrailRenderer, 掛上本腳本,運行,點按鈕,可以看到拋物線

using UnityEngine;
using System.Collections;

public class MoveTest : MonoBehaviour {
	public float angle = 80;
	bool flag = false;
	float ts;
	// Use this for initialization
	void Start () {
	//	StartCoroutine (init());
	}

	IEnumerator init(){
		yield return new WaitForSeconds (1);
		flag = true;
	}
	Vector3 pos = new Vector3(1,0,1).normalized;
	public Vector3 dir;
	// Update is called once per frame
	void Update () {
		if(!flag){
			return;
		}
		Move (Time.deltaTime);
	}

	void Move(float time ){
		//		Vector3 dir = (new Vector3; (1, 0, 1).normalized);
		//		Vector3 tempX = 2*(new Vector3(1,0,1).normalized) * Time.deltaTime * Mathf.Cos (30*Mathf.Deg2Rad);
		//		Vector3 tempY = 2*dir * Time.deltaTime * Mathf.Sin (30*Mathf.Deg2Rad)-0.5f*(9.81f)*Time.deltaTime*Time.deltaTime*Vector3.forward;
		float speed = 12;
		//		float angle = 80;
		//			//Vector3.Angle (pos.normalized, Vector3.right);
		//		Debug.LogWarning (angle);
		ts += Time.deltaTime;
		pos = new Vector3 (speed * ts * Mathf.Cos (angle*Mathf.Deg2Rad),
		                   speed * ts * Mathf.Sin (angle *Mathf.Deg2Rad) - 0.5f * (9.81f * ts * ts)
		                   ,0)*time;
		dir = dir.normalized;
		pos = new Vector3 (dir.x*pos.x,pos.y,dir.z*pos.x);
		//Debug.LogWarning (new Vector3(speed * Mathf.Cos(angle),0,(speed*Mathf.Sin(angle)-9.81f*ts)).magnitude);
		//Debug.LogWarning (speed * Mathf.Cos(angle)+","+(speed*Mathf.Sin(angle)-9.81f*ts));
		transform.position += pos;
		//60* pos.normalized*Time.deltaTime;
		//pos += Vector3.Cross (tempX, tempY) * 2;
		//Debug.LogWarning (( 2*dir * Time.deltaTime * Mathf.Sin (30)).x+","+"dir="+dir.x+","+Mathf.Sin (30*Mathf.Deg2Rad));
		//Debug.LogWarning (pos.x+","+pos.z);

	}

	void OnGUI(){
		if(GUILayout.Button("ttt")){
			flag = true;
			ts = 0;
//			StartCoroutine(MoveIE());
		}
	}

	IEnumerator MoveIE(){
		for (int i = 0; i < 500; i++) {
			Move (0.01f);
			yield return new WaitForSeconds(0.01f);
		}
	}
}


發佈了43 篇原創文章 · 獲贊 20 · 訪問量 29萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章