-
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 ;
-
}
-
}
-
}
[脚本] 转载一个抛物线的代码
最新推荐文章于 2024-07-18 21:01:07 发布