当鼠标仅在一个方向上拖动并且鼠标释放捕捉到网格时如何平滑地移动游戏对象(Unity)

时间:2019-06-27 13:38:56

标签: pointers unity3d mouseevent game-center raycasting

我正在做一个小游戏。到目前为止,我设法在x或Y方向上移动块。现在,我只是将块插入网格。我想使其平滑地跟随鼠标,并且当我释放对象时,应将其捕捉到附近的网格。我不是计算屏幕和世界位置的神。希望有经验的人来指导我。这些是Quard,而不是UI元素。

这是内部更新

 if (dragging) { //if dragging shoot raycast
   if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),
       out hit, Mathf.Infinity, blockLayer)) {
     Debug.Log("isnide dragging");
     //Parent GameObject
     //initialize empty obj and make it parent
     tempParentObj = ParentGameObjectWithNewObject();

     //calculation for movement
     if (previousPart != null) {

       Vector3 previousPos = hit.point;
       Vector3 worldPos = hit.point;
       worldPos.z = adjustedHeight;

       worldPos.x = Mathf.Round(hit.point.x);

       worldPos.y = Mathf.Round(hit.point.y);

       //Code to restrict block to change its X and y pos at the same time.
       if (worldPos.y != previousPart.transform.position.y && worldPos.x != previousPart.transform.position.x) {
         Debug.Log("returning");
         return;
       }
       //parent object of block move
       tempParentObj.transform.position = worldPos;

     }
   }

到目前为止的进展

enter image description here

我想实现的目标

enter image description here

感谢您阅读本文。任何参考将有所帮助。 谢谢。

0 个答案:

没有答案