首页 > 解决方案 > 二维轨迹路径与 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));
}

标签: c#unity3dgame-physics

解决方案


这不能仅仅因为实际箭头在您绘制弧线时使用物理原理起作用。恐怕没那么简单。

这里展示了一个非常好的预测工作示例(不是轨迹图),以及 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

~皮诺


推荐阅读