unity屏幕画线2D,3d

先设置canvas
unity屏幕画线2D,3d_第1张图片
lineRender基本保持不变
unity屏幕画线2D,3d_第2张图片

代码控制

 private void Start()
    {
        line = transform.GetComponent<LineRenderer>();
        //line.SetPosition(0,transform.position);
        //line.SetPosition(1,target.position);
    }

    public void Update()
    {
        if (Input.GetKey(KeyCode.W))
        {
            transform.localPosition+=Vector3.up;
        }
        if (Input.GetKey(KeyCode.S))
        {
            transform.localPosition+=Vector3.down;
        }
        if (Input.GetKey(KeyCode.A))
        {
            transform.localPosition+=Vector3.left;
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.localPosition+=Vector3.right;
        }
        JugePos(transform.position);
    }

    public void JugePos(Vector3 pos)
    {
        if (pos != _startPos)
        {
            _startPos = pos;
            paintPos.Add(pos);
        }
        line.positionCount = paintPos.Count;
        line.SetPositions(paintPos.ToArray());
    }

你可能感兴趣的:(unity效果,unity,游戏引擎)