先看下最终效果,下面显示的第二篇和第三篇的效果。点击地面,生成一个正方体,然后两颗球分别以匀速(第二篇)和匀加速(第三篇)的方式,以抛物线飞向目的地。

说明下本文的应用场景
已知炮台的仰角是30度,在仰角方向的速度是10m/s, 实现抛物线运动。没有太大的实用意义,仅仅是用来说明的公式的基本应用。
物体的抛物线运动,可以将仰角方向的速度分解成水平方向和垂直方向。已知仰角方向的速度,和角度,可以根据三角函数可以计算出水平方向的速度和垂直方向的速度。
在水平方向的速度是匀速运动,而在垂直方向是重力加速度的自由落体运动。
具体实现如下:
1.分解仰角方向的的速度为x方向和y方向
volocityY = Mathf.Sin(Mathf.Deg2Rad * angle) * volocity;
volocityX = Mathf.Cos(Mathf.Deg2Rad * angle) * volocity;
2.在Update函数中计算累计时间,并分别计算x和y方向的位移
水平方向是匀速 s=v*t
水平方向是匀加速自由落体,加速度是-g=-9.8m/s^2 ,s=Vi*t+0.5*a*(t^2)
accumulateTime += Time.deltaTime;
//水平方向匀速运动
float x = volocityX * accumulateTime;
//垂直方向重力加速度下落运动
float y = volocityY * accumulateTime - 9.8f * 0.5f * Mathf.Pow(accumulateTime, 2);
完整代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 已知速度和角度,在重力加速度下的抛物线运动
/// </summary>
public class GTest_01 : MonoBehaviour
{
public float volocity = 10;
public float angle = 30;
float totalTime = 0;
float volocityY = 0;
float volocityX = 0;
float accumulateTime = 0;
void Start()
{
volocityY = Mathf.Sin(Mathf.Deg2Rad * angle) * volocity;
volocityX = Mathf.Cos(Mathf.Deg2Rad * angle) * volocity;
}
//Update is called once per frame
void Update()
{
accumulateTime += Time.deltaTime;
//水平方向匀速运动
float x = volocityX * accumulateTime;
//垂直方向重力加速度下落运动
float y = volocityY * accumulateTime - 9.8f * 0.5f * Mathf.Pow(accumulateTime, 2);
Vector3 pos = new Vector3(x, y, 0);
if (pos.y >= 0)
{
//GameObject.CreatePrimitive(PrimitiveType.Sphere).transform.position = pos;
transform.position = pos;
}
}
}
8789

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



