Trivial

IEnumerator OnMouseDown()
    {
        Debug.Log(2121212);
        Vector3 targetScreenPos = Camera.main.WorldToScreenPoint(moveBreakDrill.transform.position);//三维物体坐标转屏幕坐标
        将鼠标屏幕坐标转为三维坐标,再计算物体位置与鼠标之间的距离
        var offset = moveBreakDrill.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, targetScreenPos.z));

        while (Input.GetMouseButton(0))
        {
            Debug.Log("jinruyidong");
            将鼠标位置二维坐标转为三维坐标
            Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, targetScreenPos.z);
            将鼠标转换的三维坐标再转换成世界坐标 + 物体与鼠标位置的偏移量
            var targetPos = Camera.main.ScreenToWorldPoint(mousePos) + offset;
            moveBreakDrill.transform.position = targetPos;
            yield return new WaitForFixedUpdate();//循环执行
        }
    }

  

MonoBehaviour<Behaviour<Component<Object

posted @ 2023-05-31 19:29  gatran  阅读(10)  评论(0编辑  收藏  举报