bool isDrag = false; Vector3 originDraw; Vector3 upPos; private void OnMouseDown() { //记录原始位置 originDraw = Camera.main.ScreenToWorldPoint(Input.mousePosition); upPos = originDraw; } private void OnMouseDrag() { if (!isCanSelect) return; if (UIGame.isCantClickSelectCard) return; Vector3 nPos = Camera.main.ScreenToWorldPoint(Input.mousePosition); //设置牌的配置 transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y + (nPos.y - upPos.y) * 5,transform.localPosition.z); upPos = nPos; if ((nPos - originDraw).magnitude > 0.05f) { isDrag = true; } //判断y偏移 >= 0.5就起飞 有可能没打出去,回到原位 if (Mathf.Abs(nPos.y - originDraw.y) >= 0.3f) { transform.localPosition = mPos; UICardControl.CardData = this; CardCtl.target.SendMessage(MoveDownEvent); UICardControl.CardData = null; } } private void OnMouseUp() { if (isDrag) { transform.localPosition = mPos; isDrag = false; } }
mainCamera是一个UI相机来的,如果设置一个3D相机为主相机,无论怎么转换为世界坐标都是一定的值,所以用UI相机。记录滑动两次的偏移,使用你所需要的偏移对你的物体进行控制移动就好了。注意UI相机转换的滑动的世界坐标偏移,和实际你3D相机的世界坐标是有偏移的,所以我这里乘了5啊!角度问题?