-
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-08-15 13:59:20 发布
本文介绍了一个使用Unity实现物体抛物线运动的简单方法。通过计算物体的初始速度、施加力的方向与大小,使物体能精确地从起点移动到指定的目标位置。此方案考虑了重力加速度的影响,并在每帧更新中施加必要的力。
1906

被折叠的 条评论
为什么被折叠?



