【问题标题】:Making an aim trajectory using a line renderer使用线渲染器制作瞄准轨迹
【发布时间】:2020-05-31 12:52:41
【问题描述】:

The reference of a trajectory line

嘿,我是 Unity 新手,我试图在线条渲染器的帮助下创建轨迹线,但它似乎不起作用。我在下面附上了我的运动脚本。如果有人帮助我,那将非常有帮助。

public void Update()
{

    if (Input.GetMouseButtonDown(0)) //press
    {
        startPoint = cam.ScreenToWorldPoint(Input.mousePosition);
        startPoint.z = 15;
        direction = endPoint - startPoint;
        transform.right = direction;

        for (int i = 0; i < numberOfDots; i++)
        {
            trajectoryDots[i].transform.position = calculatePosition(i * 0.01f);
        }
    }

    if (Input.GetMouseButtonUp(0)) //release
    {
        endPoint = cam.ScreenToWorldPoint(Input.mousePosition);
        endPoint.z = 15;

        force = new Vector2(Mathf.Clamp(startPoint.x - endPoint.x, minPower.x, maxPower.x), Mathf.Clamp(startPoint.y - endPoint.y, minPower.y, maxPower.y));
        rb.AddForce(force * power, ForceMode2D.Impulse);
    }

}

【问题讨论】:

  • 忽略我试图做的 for 循环而不是线渲染器,但这也不起作用。

标签: c# unity3d game-development


【解决方案1】:

我相信您不能使用 'trajectoryDots[i].transform.position' 来设置位置。 lineRenderers 有一个明确的 .SetPosition() 函数。

尝试调查一下: https://docs.unity3d.com/ScriptReference/LineRenderer.SetPositions.html

这可能会解决您的问题。

【讨论】:

    【解决方案2】:

    线渲染的所有位置都没有变换。

    所以你不能设置变换的位置。而是使用 lineRenderer.SetPosition 函数来设置线中点的位置以及索引。

    【讨论】:

    • 如果有帮助,应该始终接受并支持答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多