c# - 为什么我的线条渲染器没有出现在 Unity 2D 中

标签 c# unity3d

我正在尝试在 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

最佳答案

我想在玩家“切入”的地方创建一条轨迹

trail”这个词表示您应该使用 trail 渲染器!

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

API 引用:https://docs.unity3d.com/ScriptReference/TrailRenderer.html

回到你原来的问题:

由于 Vector2 到 Vector3 的转换,您的 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);
    }
}

关于c# - 为什么我的线条渲染器没有出现在 Unity 2D 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57246849/

相关文章:

c# - 在 C# 中生成代码块

unity3d - 不允许重定向 url localhost 和 127.0.0.1 时如何在 Unity 中从 Sign in with Apple 获取代码

c# - 如何在不旋转的情况下改变Unity中VerticalLayoutGroup的扩展方向?

c# - Unity C# 向围绕移动轴旋转的目标发射炮弹

c# - 播放任何声音文件(或至少播放普通音频文件)

c# - C#中私有(private)方法的代码风格

c# - 如何在不使用密码的情况下发送邮件

c# - Asp.net MVC 样板依赖注入(inject)不起作用

unity3d - 如何在运行时向 Unity Tilemap 添加可编写脚本的磁贴?

c# - 变换不会向上或向下旋转