二维轨迹路径与 Unity 中的对象路径不匹配
2d trajectory path doesn't match with Object path in Unity
我有一个游戏在投掷箭头之前绘制路径,但在投掷之后路径与箭头的路径不符。
enter image description here
这是路径的代码:
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
~皮诺
我有一个游戏在投掷箭头之前绘制路径,但在投掷之后路径与箭头的路径不符。
enter image description here
这是路径的代码:
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
~皮诺