Unity 抛物线算法

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Parabola : MonoBehaviour
{
    [Range(0.01f, 10.0f)]
    public float    Gravity          = 9.8f;
    public float    VerticalVelocity = 10;
    public Vector3  TargetPos        = Vector3.zero;
 
    protected Vector3    mLaunchPosition    = Vector3.zero;
    protected bool       mIsLaunching       = false;
    protected ED_Void_Go mOnArrived         = null;
    protected float      mElapseTime        = 0;
    protected float      mLaunchTime        = 0;

    void Start () 
    {
        mLaunchPosition = transform.position;
        mIsLaunching    = false;
        SuperStart();
    }

    void Update () 
    {
        if (mIsLaunching)
        {
            mElapseTime += Time.deltaTime;

            float t;
            float xv, zv;

            t = Mathf.Clamp(mElapseTime, 0, mLaunchTime);
            xv = (TargetPos.x - mLaunchPosition.x) / mLaunchTime;
            zv = (TargetPos.z - mLaunchPosition.z) / mLaunchTime;

            transform.position = mLaunchPosition + new Vector3(xv * t, VerticalVelocity * t - 0.5f * t * t * Gravity, zv * t);

            if (mElapseTime >= mLaunchTime)
            {
                mIsLaunching = false;
                if (mOnArrived != null)
                    mOnArrived(gameObject);
            }
        }

        OnUpdate();
    }

#if UNITY_EDITOR
    void OnDrawGizmosSelected()
    {
        if (mIsLaunching == false)
        {
            mLaunchPosition = transform.position;
        }

        Gizmos.color = Color.red;
        Gizmos.DrawSphere(TargetPos, 0.3f);
        float t;
        float tstep;

        t = CalcVerticalVelocityAndTime();
    
        int num = 10;

        float xv, zv;

        xv = (TargetPos.x - mLaunchPosition.x) / t;
        zv = (TargetPos.z - mLaunchPosition.z) / t;


        tstep = t / (float)(num);
        for (int i = 0; i <= num; ++i)
        {
            t = tstep * (float)i;

            Vector3 pos1, pos2;
            pos1 = mLaunchPosition + new Vector3(xv * t, VerticalVelocity * t - 0.5f * t * t * Gravity, zv * t);
            t = tstep * (float)(i + 1);
            pos2 = mLaunchPosition + new Vector3(xv * t, VerticalVelocity * t - 0.5f * t * t * Gravity, zv * t);

            Gizmos.color = Color.green;
            Gizmos.DrawSphere(pos1, 0.2f);

            if (i < num)
            {
                Gizmos.color = mIsLaunching ? Color.gray : Color.white;
                Gizmos.DrawLine(pos1, pos2);
            }
        }
    }
#endif
    //return time.
    float CalcVerticalVelocityAndTime()
    {
        float dis;
        dis = TargetPos.y - mLaunchPosition.y;

        if (dis >= 0)
        {
            // min v 
            // s = vt - 0.5 * gt^2
            // t = v/g
            // s = (v^2/2*g)
            // v^2 = 2*g*s;
            
            float minv;
            minv = Mathf.Sqrt(Gravity * dis * 2.0f);
            VerticalVelocity = Mathf.Clamp(VerticalVelocity, Mathf.Max(minv, 0.1f), float.MaxValue);
        }

        float t;
        t = 0;
        if (VerticalVelocity > 0)
        {
            float d;
            t = VerticalVelocity / Gravity;
            d = VerticalVelocity * t - 0.5f * Gravity *t*t;
            if (dis > 0 && Mathf.Abs(dis - d) < 0.01f)
            {
                return t;
            }
            else
            {
                // s = 0.5 * gt^2
                // t = sqrt(2s/g)
                dis = d - dis;
                t += Mathf.Sqrt(2.0f * dis / Gravity);
                return t;
            }
        }
        else
        {
            // vt - 0.5*gt^2 = s
            float v_g;
            v_g = (VerticalVelocity/Gravity);
            t = Mathf.Sqrt(v_g * v_g - (2.0f * dis / Gravity)) + v_g;
            return t;
        }
    }

    public float Launch(ED_Void_Go callback, Vector3 targetPos, float verticalVelocity = 1.0f, float gravity = 9.8f)
    {
        TargetPos        = targetPos;
        VerticalVelocity = verticalVelocity;
        Gravity          = gravity;
        mLaunchPosition  = transform.position;
        mOnArrived       = callback;
        mIsLaunching     = true;
        mElapseTime      = 0;
        mLaunchTime      = CalcVerticalVelocityAndTime();
        return mLaunchTime;
    }
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值