【问题标题】:Why is my line renderer not showing up in Unity 2D为什么我的线渲染器没有出现在 Unity 2D 中
【发布时间】:2019-07-29 03:24:22
【问题描述】:

我正在尝试在 Unity 2D 上创建一个水果忍者风格的游戏,并且我想创建一条跟随玩家“切割”的路径。每次用户拖动时,我都尝试实例化一个包含线条渲染器的“剪切”对象。但是,线渲染器没有出现。任何人都可以更正任何错误或提出新方法吗?

public class CreateCuts : MonoBehaviour
{
public GameObject cut;
public float cutDestroyTime;

private bool dragging = false;
private Vector2 swipeStart;

// Update is called once per frame
void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        dragging = true;
        swipeStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    }
    else if (Input.GetMouseButtonUp(0) && dragging)
    {
        createCut();
    }
}

private void createCut()
{
    this.dragging = false;
    Vector2 swipeEnd = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    GameObject cut = Instantiate(this.cut, this.swipeStart, Quaternion.identity) as GameObject;
    cut.GetComponent<LineRenderer>().positionCount = 1 ;
    cut.GetComponent<LineRenderer>().enabled = true;
    cut.GetComponent<LineRenderer>().SetPosition(0, this.swipeStart);
    cut.GetComponent<LineRenderer>().SetPosition(1, swipeEnd);
    Vector2[] colliderPoints = new Vector2[2];
    colliderPoints[0] = new Vector2(0.0f, 0.0f);
    colliderPoints[1] = swipeEnd - swipeStart;
    cut.GetComponent<EdgeCollider2D>().points = colliderPoints;
    Destroy(cut.gameObject, this.cutDestroyTime);
}

}

我希望会有一条线,但什么也没有出现。还有一条警告指出 SetPosition(1, swipeEnd) 超出范围。

编辑:这是我剪切对象的设置

1st part of cut object settings

2nd part of cut object settings

Positions tab of line renderer

【问题讨论】:

  • positionCount = 1一行只有1分?
  • 要不要改成2?
  • 2 代表 1 行,3 代表 2 行,依此类推。
  • 我将位置计数更改为 2,但该行仍然没有显示。
  • 另一种选择是创建一个粒子对象,所以当你的剪切对象移动时,它会随之移动,你会有一种轨迹的效果。

标签: c# unity3d


【解决方案1】:

我想创建一个轨迹,跟随玩家“切入”的位置。

trail”这个词表示您应该使用trail renderer!

手册:https://docs.unity3d.com/Manual/class-TrailRenderer.html

API 参考:https://docs.unity3d.com/ScriptReference/TrailRenderer.html

回到你原来的问题:

由于 Vector2 到 Vector3 的转换,您的 linerenderer 可能已渲染但在随机位置,我不知道您的项目结构,但可能是这种情况。

请张贴一张带有您剪切的游戏对象的图片,该对象包含您的 linerenderer,并扩展 linerenderer 上的位置选项卡,以便我们可以看到您的点 xyz 坐标

还应用评论者提到的更改,因为您确实需要 2 个顶点来表示一行:P

更新:

public class CreateCuts : MonoBehaviour
{
    public GameObject cut;
    public float cutDestroyTime;

    private bool dragging = false;
    private Vector3 swipeStart;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            dragging = true;
            swipeStart = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Debug.Log("Swipe Start: " + swipeStart);
        }
        else if (Input.GetMouseButtonUp(0) && dragging)
        {
            createCut();
        }
    }

    private void createCut()
    {
        this.dragging = false;
        Vector3 swipeEnd = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Debug.Log("SwipeEnd: " + swipeEnd);
        GameObject cut = Instantiate(this.cut, swipeStart, Quaternion.identity);
        cut.GetComponent<LineRenderer>().positionCount = 2;
        // why is it not enabled by default if you just instantiate the gameobject O.o?
        cut.GetComponent<LineRenderer>().enabled = true;
        cut.GetComponent<LineRenderer>().SetPositions(new Vector3[]{
            new Vector3(swipeStart.x, swipeStart.y, 10),
            new Vector3(swipeEnd.x, swipeEnd.y, 10)
            // z is zero cos we are in 2d in unity up axis is Y we set it to 10 for visibility reasons tho}
        });
        // Commented out cos atm we are "debugging" your linerenderer
        // Vector2[] colliderPoints = new Vector2[2];
        // colliderPoints[0] = new Vector2(0.0f, 0.0f);
        // colliderPoints[1] = swipeEnd - swipeStart;
        // cut.GetComponent<EdgeCollider2D>().points = colliderPoints;
        //Destroy(cut.gameObject, this.cutDestroyTime);
    }
}

【讨论】:

  • 我一定会研究追踪渲染器,谢谢!另外,如何将图片发布到 cmets 中?抱歉,这是我第一次真正使用堆栈溢出
  • 在graficon上方的第一张图片上有一个名为Positions的文字,点击它旁边的箭头,我们可以看到点坐标:P
  • hahahahah 抱歉,我以为您指的是其他职位选项卡。刚刚添加了
  • 所以从图片中我们可以看到你有一条线,但它是从 (0,0,0) 到 (0,0,1),它是在那个位置渲染的吗?我猜这不是你想要的位置
  • 根本没有渲染线,是的,这不是我想要的位置:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-09
  • 1970-01-01
  • 1970-01-01
  • 2019-06-30
  • 2013-07-10
  • 1970-01-01
相关资源
最近更新 更多