using UnityEngine;using System.Collections;using System;public class ParabolaNode : MonoBehaviour { //重力加速度 public const float G = 9.8f; // 物體的初速度 public float V0 { set; get; } // 物體初速度與水平方向的夾角 public float Sita { set; get; } // 最大射程 public float Smax { set; get; } // 最大高度 public float H { set; get; } // 已耗用時間 public float T { set; get; } // 起點 public Vector3 srcPos = new Vector3(); // 目標 public Vector3 dstPos = new Vector3(); // 移動中 public bool bMoving { set; get; } // 移動等待時間 public float fWaitSecond { set; get; } // 高度限制 public float fHighLimit { get; set; } void Update() { // wait if (fWaitSecond > 0) { fWaitSecond -= Time.deltaTime; return; } UpdateObj(Time.deltaTime); } public void Init(float fV0, float fSita, float fHigh, Vector3 vtDst, float fWait) { fWaitSecond = fWait; // fHighLimit = fHigh; // 初始 V0 = fV0; // 角度 Sita = fSita; // Mathf.PI / 4 // 目標 dstPos.x = vtDst.x; dstPos.y = vtDst.y; dstPos.z = vtDst.z; // 已耗用時間 T = 2 * V0 * (float)Math.Sin(Sita) / G; // 最大高度 H = V0 * V0 * (float)Math.Sin(Sita) * (float)Math.Sin(Sita) / (2 * G); // 最大射程 Smax = 2 * V0 * V0 * (float)Math.Sin(Sita) * (float)Math.Cos(Sita) / G; } private float nowT = 0.0f; private void UpdateObj(float pass) { if (T < nowT) return; nowT += pass; // 水平座標 float x = V0 * (float)Math.Cos(Sita) * nowT; // 豎直座標 float y = V0 * (float)Math.Sin(Sita) * nowT - G * nowT * nowT / 2; Vector3 vt = gameObject.transform.localPosition; vt.x = (dstPos.x - srcPos.x) / Smax * x; vt.y = (fHighLimit / H -1) * y; double fAng = (double)Vector3.Angle(srcPos, dstPos); vt.z = (float)Math.Sin(fAng) * vt.x; gameObject.transform.localPosition = vt; }}