Unity绘制抛物线

Unity绘制抛物线<25/11/2017>

获取GetMouseButtonDown初始点和GetMouseButton实时点的向量长度,替换里面的抛物线最高高度,进行微调可以弄出自己想要的效果

实际运用效果如下:


using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[ExecuteInEditMode]
public class Parabola : MonoBehaviour {
    //重力
    [Range(0,1)]
    public float gravity = 0.13f;
    //最大长度
    public float maxLength = 50;
    //两点之间的距离
    const float length = 0.2f;
    //点集合
    List&l
### 如何在 Unity 中实现抛物线轨迹计算或绘制 #### 基于物理公式的抛物线轨迹计算 对于无需复杂物理交互的场景,可以采用基于物理公式的抛物线轨迹计算。这种方法性能较高且易于实现。通过给定初始速度、发射角度以及重力加速度等因素,可以直接利用物理学中的运动方程来预测物体的位置变化。 假设已知初速度 \(v_0\) 和发射角 \(\theta\), 可以按照下面的方式求解任意时刻 t 的位置坐标 (x, y): \[ x(t) = v_{0} * cos(θ) * t \] \[ y(t) = h + v_{0} * sin(θ) * t - 0.5g*t^{2} \] 其中 g 表示重力加速度常数,默认情况下取9.8m/s²,在Unity中通常设置为-9.81f用于向下的方向;h 是起始高度[^1]。 ```csharp public class ParabolicTrajectory : MonoBehaviour { public Vector3 startPoint; public float initialSpeed = 10f; public float angleInDegrees = 45f; void Update(){ DrawParabola(); } private void DrawParebra(){ // Convert degrees to radians and calculate components of velocity vector. var radianAngle = Mathf.Deg2Rad * angleInDegrees; var vx = Mathf.Cos(radianAngle)*initialSpeed; var vy = Mathf.Sin(radianAngle)*initialSpeed; for(float time=0;time<=flightTime;time+= Time.deltaTime){ var positionX = startPoint.x + vx*time; var positionY = startPoint.y + vy*time - 0.5f*Physics.gravity.magnitude*Mathf.Pow(time,2); Debug.DrawLine(new Vector3(positionX,positionY,startPoint.z), new Vector3(positionX+vx*Time.deltaTime, positionY+(vy-Time.deltaTime*Physics.gravity.magnitude)*Time.deltaTime, startPoint.z)); } float flightTime => (-vy + Mathf.Sqrt(vy*vy + 2* Physics.gravity.magnitude*(startPoint.y-endPoint.y))) / Physics.gravity.magnitude; } } ``` 此代码片段展示了如何创建一条从`startPoint`出发并遵循指定的速度和角度形成的理想化抛射路径。注意这里使用了`Debug.DrawLine()`来进行可视化调试目的,并不是实际游戏逻辑的一部分[^2]。 #### 物体沿预定义抛物线移动 当希望让某个刚体对象严格按照预定好的抛物线路径前进时,则需确保该物体绑定了相应的控制脚本(比如命名为 `ParabolaMovement`) 并设置了起点(`startPoint`)终点(`endPoint`)参数值。此外还要确认`MoveOnParabola()` 方法确实被执行以便启动动画过程[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值