unity抛物线,平均速度下的运动轨迹

以前分享了关于两点之间抛物线的“金手指”的实现方案,而后有朋友问我,通常状况下会给出速度,如何模拟天然的轨迹。html

我一听这不是很容易实现么,根据以前的公式,得出两点之间时间恒定时,轨迹是肯定的,也就是说平均速度是恒定。算法

那么反过来,在给定平均速度,而后再经过距离/速度,就可得出时间,那么轨迹也就肯定了。测试

OK,我很少废话,直接上代码:this

using UnityEngine;
using System.Collections;

public class PaoWuLine : MonoBehaviour
{
    public float ShotSpeed = 10;
    private float time = 1;//表明从A点出发到B通过的时长
    public Transform pointA;//点A
    public Transform pointB;//点B
    public float g = -10;//重力加速度
    // Use this for initialization
    private Vector3 speed;//初速度向量
    private Vector3 Gravity;//重力向量

    private Vector3 currentAngle;
    void Start()
    {
        time = Vector3.Distance(pointA.position, pointB.position)/ShotSpeed;
        transform.position = pointA.position;//将物体置于A点
        //经过一个式子计算初速度
        speed = new Vector3((pointB.position.x - pointA.position.x) / time,
            (pointB.position.y - pointA.position.y) / time - 0.5f * g * time, (pointB.position.z - pointA.position.z) / time);
        Gravity = Vector3.zero;//重力初始速度为0
    }
    private float dTime = 0;
    // Update is called once per frame
    void FixedUpdate()
    {

        Gravity.y = g * (dTime += Time.fixedDeltaTime);//v=at
        //模拟位移
        transform.position += (speed + Gravity) * Time.fixedDeltaTime;
        currentAngle.x = -Mathf.Atan((speed.y + Gravity.y) / speed.z) * Mathf.Rad2Deg;
        transform.eulerAngles = currentAngle;
    }
}

此次直接把角度加上了,喜欢的朋友能够本身测试。spa

时间恒定的两点轨迹:http://www.cnblogs.com/jqg-aliang/p/4806017.htmlcode

通常抛物线轨迹:http://www.cnblogs.com/jqg-aliang/p/4806002.html#3292517orm

好了,关于愤怒的小鸟,弓箭之类的简单实现算法差很少够用了。以前我研究了好久的导弹飞行算法有了新的方向,htm

使用抛物线模拟动态轨迹,将会有更加真实天然的效果。欢迎关注。blog

相关文章
相关标签/搜索