【问题标题】:2d trajectory path doesn't match with Object path in Unity二维轨迹路径与 Unity 中的对象路径不匹配
【发布时间】:2023-04-04 07:05:01
【问题描述】:

我有一个游戏,在投掷箭头之前绘制路径,但路径与投掷后的箭头路径不匹配。

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));
}

【问题讨论】:

  • 没有深入研究您的代码,但是如果您使用刚体,则托盘不应是圆形,而是抛物线
  • @rustyBucketBay 圆的轨迹在哪里?您指的是沿路径绘制的圆圈吗?
  • 是的,这个函数叫做draw_circle,你传给它一个中心和一个可变半径,这就是为什么我假设你在哪里画一个圆。
  • @rustyBucketBay 是的,之所以这样称呼是因为正在绘制圆圈。可以毫无问题地在抛物线路径上绘制圆心。 draw_circle 没有绘制路径的主要线索是它不需要初始速度

标签: c# unity3d game-physics


【解决方案1】:

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

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

~皮诺

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 2020-04-18
    相关资源
    最近更新 更多