Unity3D 拋物線實現

來源:互聯網
上載者:User
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;    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.