Unity中实现 抛物线运动

抛物线的实现有很多种,
1.利用 y=sin(x) 函数 ,该函数 分布图就是 个抛物线。
在这里插入图片描述
只要控制在 0到PI 之间 就可以得到向下的抛物线方程。x可以是个增量;
不过这样得到 会比较难控制 开口弧度。 参数比较难调整;

第二种,使用重力加速度模拟抛物线的。

在 Awake 中初始化速度,重力
// 时间=距离/速度
time = Vector3.Distance(pointA.position, pointB.position)/ShotSpeed;
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);

在Update中
计算 重力
Gravity.y = g * (dTime += Time.fixedDeltaTime);

//更新位置
transform.position += (speed + Gravity) * Time.fixedDeltaTime;

//角度
currentAngle.x = -Mathf.Atan((speed.y + Gravity.y) / speed.z) * Mathf.Rad2Deg;

currentAngle.x = -Mathf.Atan((speed.y + Gravity.y) / speed.z) * Mathf.Rad2Deg;

https://blog.csdn.net/sinat_20559947/article/details/53389157?utm_source=blogkpcl1

这种方法 并不稳定,而且还有很多问题。
参数不好控制,容易出问题,当然如果参数调正好是没问题的。
比较繁琐。 不太适合二维空间,适合3维 空间。

第三种是直接根据两点之间 计算出一个抛物线公式

y=ax²+bx+c

    void Init()
    {
        x1 = PointA.position.x;
        x2 = PointB.position.x;
        y1 = PointA.position.y;
        y2 = PointB.position.y;

        float widgh = Mathf.Abs(x1 - x2);
        y3 = y1 - H; // 得到  顶点坐标;
        //顶点 公式 y=a(x-h)²+k   y=ax²-2ahx+ah²+k
        //焦点 公式  y=(x-x1)(x-x2)   x1 x2是 x轴的交点


        x3 = x1 + widgh/2;
        b = ((y1 - y3) * (x1 * x1 - x2 * x2) - (y1 - y2) * (x1 * x1 - x3 * x3)) / ((x1 - x3) * (x1 * x1 - x2 * x2) - (x1 - x2) * (x1 * x1 - x3 * x3));
        a = ((y1 - y2) - b * (x1 - x2)) / (x1 * x1 - x2 * x2);
        c = y1 - a * x1 * x1 - b * x1;

        vx = widgh / speed;
    }

在 update中 直接 使用公式计算 就好

            float x = PointA.position.x + Time.deltaTime * vx;
            float y = a * Mathf.Pow(x, 2) + b * x + c;
            PointA.position = new Vector3(x, y);           

这种 方法就比较精确, 如果是在3维空间的话,计算就有点复杂,不过 原理还是 一样的。 两点之间的宽度 可以用vector3.Distance代替;
可能这种方法就不太适合 3维空间了,计算复杂了点。但也比较精确。

还有一种 方法就是 使用forward去计算;然后 使用LookAt 计算方向,向它 运动过去。

 			Vector3 PointBPos = PointB.transform.localPosition;
          

            this.PointA.transform.LookAt(PointBPos);
           
            float angle = Mathf.Min(1, Vector3.Distance(PointA.transform.localPosition, PointBPos) / distanceToPointB) * Aniangle;
         
            this.PointA.transform.localRotation = PointA.transform.localRotation * Quaternion.Euler(Mathf.Clamp(-angle, -42, 42), 0, 0);
            
            float currentDist = Vector3.Distance(PointA.transform.localPosition, PointB.transform.localPosition);
			this.PointA.transform.Translate(Vector3.forward * Mathf.Min(speed * Time.deltaTime, currentDist)+Vector3.up*ySpeed);

      
  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Unity实现物体按抛物线运动的代码示例: ```csharp public class ParabolicMotion : MonoBehaviour { // 起始点、终止点和高度 public Transform startPoint; public Transform endPoint; public float height; // 运动时间 public float timeToReachTarget = 1f; private bool isMoving = false; private float timer = 0f; private Vector3 startPosition; private Vector3 endPosition; void Start() { // 获取起始点和终止点的位置 startPosition = startPoint.position; endPosition = endPoint.position; } void Update() { if (isMoving) { // 计算当前时间与总时间的比例 float t = timer / timeToReachTarget; // 计算当前位置 Vector3 currentPos = ParabolicInterpolation(startPosition, endPosition, height, t); // 更新物体位置 transform.position = currentPos; // 更新计时器 timer += Time.deltaTime; // 判断是否到达终点 if (timer >= timeToReachTarget) { isMoving = false; timer = 0f; } } } public void StartMotion() { isMoving = true; } // 抛物线插值函数 private Vector3 ParabolicInterpolation(Vector3 start, Vector3 end, float height, float t) { Vector3 result = Vector3.zero; // 计算抛物线顶点位置 Vector3 vertex = new Vector3((start.x + end.x) / 2f, height, (start.z + end.z) / 2f); // 计算当前位置 result.x = Mathf.Lerp(start.x, end.x, t); result.y = Mathf.Lerp(start.y, end.y, t) + Mathf.Sin(t * Mathf.PI) * height; result.z = Mathf.Lerp(start.z, end.z, t); return result; } } ``` 在这个示例,我们定义了起始点、终止点和高度等参数,然后在Update函数计算当前位置,利用抛物线插值函数实现物体按抛物线运动。我们还通过StartMotion函数来启动运动,可以通过其他事件来触发该函数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值