GameObject跟随鼠标无法正常工作

时间:2019-06-12 11:00:26

标签: c# unity3d

我有一个附加到对象的脚本,该脚本必须将对象与鼠标一起移动。就像我单击对象一样,它会重置为鼠标的位置,并且不考虑偏移量。

private void Update()
{
    Vector3 moveTo = EDUCam.Instance.attachedCamera.ScreenToWorldPoint(EDUCam.Instance.mousePosition);
    moveTo.z = 0f;
    if (canDrag)
    {
        if (Input.GetMouseButtonDown(0))
            offset = transform.position - moveTo;

     if (horizontal)
        {
            moveTo.x = Mathf.Clamp(moveTo.x, 0f, 5.5f);
            //transform.localPosition = new Vector3(moveTo.x, transform.localPosition.y) + offset;
            gameObject.GetComponent<Rigidbody2D>().MovePosition(new Vector2(moveTo.x + offset.x, transform.position.y));
        }
    }
}

0 个答案:

没有答案