∮紫∈寒§ 发表于 2012-11-26 14:02

分享一个抛物线的代码

第一次发帖子,请大家多多指教。
想做一个抛物线的算法,去网上搜也有一些,但是都不是我想要的那样的效果,所以只好自己动手,当中有许多公式还是问的朋友(自己早就忘记了)。希望这段代码能给大家帮助,如果有什么写的不好的请大家多多指教。
用法很简单:1. 代码添加到要抛出的物体 2.设置 M(质量)、G(重力)、TUP(希望到达最高点的时间 )和TARGET(目的地物体。当然这个抛物线只是简单写了下,计算的只是在完美的运动下的抛物线。所以需要注意几个问题:1.摩擦力要为0 2.物理材质可能会有影响

求好评啊亲们~~~~~using UnityEngine;
using System.Collections;

public class test : MonoBehaviour {
    public float m = 2, g = 9.81f, tUp = 0.5f;//m:质量 g:重力 tup:希望到达最高点的时间
    private float forceUp, forceRight, vUp0, highest, tUpTotal, vRight;//forceup: 垂直方向的力 vup0:垂直初速度highest: 最高点 tUpTotal: 总时间
    public Transform target;//目标位置物体
    public bool isParabola;
    // Use this for initialization
    void Start()
    {
      vUp0 = g * tUp;//求出锤子的初速度
      forceUp = g + vUp0 * m / Time.fixedDeltaTime;//垂直方向添加的力
      highest = transform.position.y + 0.5f * g * tUp * tUp;//计算最高点的高度
      tUpTotal = Mathf.Sqrt(Mathf.Abs(target.position.y - highest) * 2 / g) + tUp;//总时间计算
      vRight = (target.position.x - transform.position.x) / tUpTotal;//求出水平速度
      forceRight = vRight / Time.fixedDeltaTime * m;//水平方向添加的力
    }
    void FixedUpdate()
    {
      if (isParabola)
      {
            isParabola = false;
            rigidbody.AddForce(Vector3.up * forceUp);
            rigidbody.AddForce(Vector3.right * forceRight);
      }
    }
    void OnGUI()
    {
      if (GUILayout.Button("dsfsd"))
      {
            isParabola = true;
      }
    }
}

∮紫∈寒§ 发表于 2012-11-26 14:16

无人回复自己顶起

∮紫∈寒§ 发表于 2012-11-26 14:16

{:5_404:}{:5_404:}{:5_404:}{:5_404:}{:5_404:}{:5_404:}{:5_404:}{:5_404:}{:5_404:}

ペ猥小琐ぺ 发表于 2012-11-26 15:13

{:5_404:}{:5_404:}{:5_404:}{:5_404:}

∮紫∈寒§ 发表于 2012-11-26 15:22

有用不错很好 谢谢了

542491514 发表于 2013-5-20 13:47

不是给个力推一推它,它就可以抛物线移动的吗

aaabbbsss12345 发表于 2014-5-27 11:14

谢谢 分享 谢谢

ep43ds3 发表于 2017-2-17 11:52

很不错

ep43ds3 发表于 2017-2-17 12:23

楼主是超人

hxsdup6 发表于 2017-2-17 11:37

顶顶多好
页: [1]
查看完整版本: 分享一个抛物线的代码