将动态线渲染器附加到GameObject

时间:2017-12-13 13:39:33

标签: c# unity3d

我创建了一个包含列表的(3D)扫描图像。画布位于世界空间。当它通过选择GameObject激活时,它也一直面向摄像机。现在我想在画布的左上角和RaycastHit /所选对象的点之间添加一个动态线渲染器(参见草图)。 GO侧的LR位置应该是静态的,列表侧的LR应该是动态的。因此,即使画布开始在所有轴(x,y,z)上旋转,它也应该保持在角落处。

sketch

我已经坚持列表方面LR的定位。我没有得到角落坐标来附加LR。我已经尝试了边界方法和其他一些方法,但我没有取得成功。到目前为止我的代码:

//GUI elements
public Canvas canvas_list;
public GameObject content_list;

LineRenderer lr_list;

// Use this for initialization
void Start ()
{
    Setup_LR();
}

// Update is called once per frame
void Update ()
{
    // Canvas facing to the camera
    canvas_list.transform.rotation = ar_camera.transform.rotation;

    // LineRenderer is always attachted to canvas (Part 1)
    lr_list.SetPosition(1, new Vector3(canvas_list.GetComponent<Collider>().bounds.min.x, canvas_list.GetComponent<Collider>().bounds.max.y, canvas_list.GetComponent<Collider>().bounds.min.z));

    // Selecting objects
    if (Input.GetMouseButtonDown(0))
    {
        Selecting_Objects();
    }
}

void Selecting_Objects()
{
    raycastHit = new RaycastHit();   

    if (Physics.Raycast(ar_camera.ScreenPointToRay(Input.mousePosition), out raycastHit))
    {
        if (!EventSystem.current.IsPointerOverGameObject())
        {
            GameObject selected_object = raycastHit.collider.gameObject;

            // LineRenderer is always attachted to building component (Part 2) - avoids that the line renderer gets attached to GUI element
            lr_list.SetPosition(0, raycastHit.point);
        }
    }
}


void Setup_LR()
{
    canvas_list.transform.gameObject.AddComponent<BoxCollider>();

    lr_list = new GameObject().AddComponent<LineRenderer>();
    lr_list.material.color = Color.red;
    lr_list.startWidth = 0.01f;
    lr_list.endWidth = 0.01f;
}

现在看起来如何: enter image description here 谢谢!

0 个答案:

没有答案