c# - 二维轨迹路径与 Unity 中的对象路径不匹配
问题描述
我有一个游戏,在投掷箭头之前绘制路径,但投掷后路径不适合箭头路径。
这是路径的代码:
public void DrawThePath()
{
cm.clear_circles();// cm is object for drawing circle
float x = transform.position.x;
float y;
for (int i=0; i<circleNumbers; i++)
{
y = calculateHeight(x - Arrow.transform.position.x);
cm.draw_circle(x, y, circleRedius);
x += circleXdistance;
if (x > Aim.transform.position.x)
{
break;
}
}
x = Aim.transform.position.x;
y = calculateHeight(x - Arrow.transform.position.x);
cm.draw_circle(x, y, circleRedius);
}
private float calculateHeight(float x)
{
float g = Physics2D.gravity.y;
float theta = Arrow.transform.rotation.z * 2 / 3 * Mathf.PI;
float velocity = FindObjectOfType<HandMove>().getVelocity();
float initialY = Arrow.transform.position.y;
float y = g / 2 * Mathf.Pow(x / (Mathf.Cos(theta) * velocity), 2);
y += Mathf.Tan(theta) * x;
y += initialY;
return y;
}
和投掷箭头的代码:
public void throwArrow()
{
velocity = FindObjectOfType<HandMove>().getVelocity();
GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
GetComponent<Rigidbody2D>().velocity = new Vector2(velocity * Mathf.Cos(Angle), velocity * Mathf.Sin(Angle));
}
解决方案
这不能仅仅因为实际箭头在您绘制弧线时使用物理原理起作用。恐怕没那么简单。
这里展示了一个非常好的预测工作示例(不是轨迹图),以及 Unity 项目演示:https ://www.forrestthewoods.com/blog/solving_ballistic_trajectories/它可以为您提供有关物理学如何工作的良好背景。
还有一个旧但仍然有效的代码可以解决您在 Unity Wiki 上的特定请求:http ://wiki.unity3d.com/index.php?title=Trajectory_Simulation&_ga=2.144123763.792205966.1600849394-1294758123.1579039644
~皮诺
推荐阅读
- javascript - 数组内参数的Javascript if语句
- html - 为什么子填充会扩大父宽度?
- tensorflow - 来自 conda 的 Tensorflow-gpu 2.5.0 无法识别 GPU
- reactjs - Formik 的“isSubmitting”何时设置回 False?
- flutter - 每个页面底部的持久小部件
- node.js - 如何使用从数据库中获取的唯一用户 ID 来调用带有 socket.io 的用户
- ios - 将委托协议一致性添加到纯 swift 类
- javascript - 如何测试反应下拉表单
- echarts - 基于Echart数据点击事件重定向窗口位置
- python - 如何从列表中获取一个值并遍历一个单独的字符串以找到该值所在的索引?