为什么通过移动触摸通过触摸将对象拖动到触摸位置,而不是在上一次触摸和当前触摸之间移动偏移量?

时间:2020-06-16 13:14:25

标签: c# android unity3d touch

所以这对我来说毫无意义。假定游戏对象(地图)移动了最后触摸位置和当前触摸位置之间的差。它首先将其位置更改为现在要触摸的每个位置,然后按照预期的那样移动。它会瞬移。我希望它仅移动偏移量。它与PC上的鼠标输入完美配合,因此这使我感到困惑。自从我编码以来已经有一段时间了,所以这可能有点愚蠢。这是一些代码:

        if (touch)
        {
            int nbTouches = Input.touchCount;

            if (nbTouches > 0)
            {
                for (int i = 0; i < nbTouches; i++)
                {
                    Touch touch = Input.GetTouch(i);

                    TouchPhase phase = touch.phase;


                    switch (phase)
                    {
                        case TouchPhase.Began:
                            timeTouched = Time.time;
                            touchPos = touch.position;
                            if (G.mapIsOn)
                            {
                                if (!holding)
                                {
                                    holding = true;
                                    Vector3 touchDownPos = touchPos;
                                    touchDownPos.z = Camera.main.nearClipPlane;
                                  //  touchDownPos.z = Camera.main.nearClipPlane - MapController.MC.mapBackground.transform.position.z;
                                    touchDownPos = Camera.main.ScreenToWorldPoint(touchDownPos);
                                    lastTouchPos = touchDownPos;
                                }

                            }
                            break;
                        case TouchPhase.Moved:
                            if (G.mapIsOn && holding)
                            {
                                Vector3 newPos = touch.position;
                                newPos.z = Camera.main.nearClipPlane;
                            //    newPos.z = Camera.main.nearClipPlane - MapController.MC.mapBackground.transform.position.z;
                                newPos = Camera.main.ScreenToWorldPoint(newPos);

                         //       if (lastTouchPos != newPos)
                         //       {
                                    if (onHoldMove != null)
                                    {
                                        onHoldMove(newPos, lastTouchPos);
                                    }
                        //        }
                               /* if (onHoldMoveDelta != null)
                                {
                                    onHoldMoveDelta(touch.deltaPosition);
                                }*/

                                //     mapBackground.transform.position = newMapPos;
                                lastTouchPos = newPos;
                            //    lines.ScaleLines();
                                //  Debug.Log("Moving: " + newPos);
                            }
                            //   print("Touch index " + touch.fingerId + " has moved by " + touch.deltaPosition);
                            break;
                        case TouchPhase.Stationary:
                            //   print("Touch index " + touch.fingerId + " is stationary at position " + touch.position);
                            break;
                        case TouchPhase.Ended:
                         //   if (G.mapIsOn)
                       //     {
                                if (holding) holding = false;
                       //     }
                            if (Time.time - timeTouched <= tapTime)
                            {
                                tapped = true;
                            }
                            break;
                        case TouchPhase.Canceled:
                            //   print("Touch index " + touch.fingerId + " cancelled");
                            break;
                    }
                }

对不起,如果格式已关闭。同样抱歉,注释行过多,您会看到我一直在尝试许多不同的“修复”。

    public void HandleOnHoldMove(Vector3 newPos, Vector3 lastPos)
    {
        Vector3 offset = lastPos - newPos;
        Vector3 mapPos = mapBackground.transform.position;
        Vector3 newMapPos = new Vector3(mapPos.x - offset.x * mapDragBoost, mapPos.y - offset.y * mapDragBoost, mapPos.z);
        mapBackground.transform.position = newMapPos;
        lines.ScaleLines();
    }

2 个答案:

答案 0 :(得分:0)

正如我期望的那样,这是愚蠢的,是我的鼠标输入未正确放置的支架。触摸和鼠标都用作输入。出于某种原因,鼠标出于某种原因充当了触摸输入,我忘记了封装地图移动代码,以便从触摸控制器输入鼠标。如果有人感到困惑为什么此代码不起作用,将很快删除此问题。

答案 1 :(得分:0)

触摸输入可立即更改位置。 例如,您的鼠标无法松动屏幕,但是您可以用触摸设备上的手指。

尝试使用Vector3.Lerp在新位置和新位置之间进行平滑过渡,

transform.position = Vector3.lerp(transform.position, //New position, //move speed);