如何解决2d轨迹路径与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,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
〜皮诺
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。