简体   繁体   English

2d轨迹线预测不准确

[英]2d Trajectory Line Prediction not acurate

I'm current facing a problem where in my trajectory line is very inaccurate 我目前面临一个问题,即我的轨迹线非常不准确

Please see the attached image below 请参阅下面的附图

在此输入图像描述

Here's where it goes 这就是它的发展方向

在此输入图像描述

Here's my code so far 到目前为止,这是我的代码

private void Start()
    {
        line = new GameObject[maxDots];
        for (int i = 0; i < line.Length; i++)
        {
            var go = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            go.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
            line[i] = go;
        }

        startPos = transform.position;
    }

on my OnMouseUp is for shooting the ball 我的OnMouseUp用于射击球

    //TEMPORARY
    private void OnMouseUp()
    {
        // Disable IsKenematic
        GetComponent<Rigidbody2D>().isKinematic = false;

        // Add the Force
        Vector2 dir = startPos - (Vector2)transform.position;
        GetComponent<Rigidbody2D>().AddForce(dir * force);
        //Remove the script (not the gameobject)
        Destroy(this);
    }

And on my OnMouseDrag is for the ball to keep in radius cause I set a limit for the dragging of the ball 而我的OnMouseDrag是让球保持半径,因为我设置了拖球的限制

    private void OnMouseDrag()
    {
        DisplayLine();
        //Convert mouse potision to world position
        Vector2 p = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        //Keep it in a certain radius
        float radius = 1.8f;
        Vector2 dir = p - startPos;

        if (dir.sqrMagnitude > radius)
        {
            dir = dir.normalized * radius;

            //Set position
            transform.position = startPos + dir;
        }
    }

Here's my method of displaying the line 这是我显示线条的方法

    void DisplayLine()
    {
        line[0].transform.position = transform.position;
        Vector3 v3 = transform.position;
        float y = (forces * (home - transform.position)).y;
        float t = 0.0f;
        v3.y = 0.0f;

        for(int i = 1; i < line.Length; i++)
        {
            v3 += forces * (home - transform.position) * spacing;
            t += spacing;
            v3.y = y * t + 0.5f * Physics2D.gravity.y * t * t + transform.position.y;
            line[i].transform.position = v3;
        }
    }

What I am trying to do is that create a trajectory prediction line for my game I know I am almost there but still couldn't get the exact output that I want. 我想要做的是为我的游戏创建一个轨迹预测线我知道我几乎在那里但仍然无法得到我想要的确切输出。 Could someone help me. 有人能帮助我吗 Thank you. 谢谢。

Use the same function to calculate both. 使用相同的函数来计算两者。 Then you won't have the problem also any change on trajectory calculation results in only one change not two. 那么你也不会遇到问题,轨迹计算的任何变化都只会导致一次变化而不是两次。

Solved it 解决了它

line[0].transform.position = transform.position;
        Vector2 v2 = transform.position;
        float y = (force *( startPos - (Vector2)transform.position)).y;
        float t = 0.0f;
        v2.y = 0.0f;

        for(int i = 1; i < line.Length; i++)
        {
            v2 += force * (startPos - (Vector2)transform.position) * spacing;
            t += spacing;
            v2.y = y * t + 0.5f * Physics2D.gravity.y * t * t + transform.position.y;
            line[i].transform.position = v2;
        }

I need to change from vector3 to vector2 so I need to cast (vector2) between those transform.positions.y 我需要从vector3更改为vector2所以我需要在transform.positions.y之间转换(vector2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM