Unity3d 触摸滑动

Unity3d Touch Swipe

如果玩家触摸,他会跳跃并滑动(左-右只是从左向右移动......就像任何跑步者一样)。 一切似乎都很好,但一如既往有一个但是 这是代码:

Vector3 startPos;
float minSwipeDistX, minSwipeDistY;
bool isJump = false;

void Start()
{
    minSwipeDistX = minSwipeDistY = Screen.width / 6;
}

bool isSwipe = false;
bool isTouch = false;
void Update()
{
    if (Input.touchCount > 0)
    {
        Touch touch = Input.touches[0];
        switch (touch.phase)
        {
            case TouchPhase.Began:
                startPos = touch.position;
                break;
            case TouchPhase.Moved:
                isTouch = true;
                float swipeDistHorizontal = (new Vector3(touch.position.x, 0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
                float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
                if (swipeDistHorizontal > minSwipeDistX)
                {
                    float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
                    if (swipeValue > 0 && !isSwipe)//to right swipe
                    {
                        isTouch = false;
                        isSwipe = true;
                        Debug.Log("Right");
                    }
                    else if (swipeValue < 0 && !isSwipe)//to left swipe
                    {
                        isTouch = false;
                        isSwipe = true;
                        Debug.Log("Left");
                    }
                }
                // add swipe to up
                if (swipeDistVertical > minSwipeDistY)
                {
                    float swipeValueY = Mathf.Sign(touch.position.y - startPos.y);
                    if (swipeValueY > 0 && !isSwipe)
                    {
                        isTouch = false;
                        isSwipe = true;
                        Debug.Log("Up");
                    }
                }
                break;
            case TouchPhase.Stationary:
                isJump = true;
                break;
            case TouchPhase.Ended:
            case TouchPhase.Canceled:
                isTouch = false;
                isSwipe = false;
                break;
        }
    }
}

void FixedUpdate()
{
    if (isJump && !isTouch)
    {
        Debug.Log("Tap");
        isJump = false;
    }
}

进行简单的触摸时 (TouchPhase.Stationary) 会立即对玩家跳跃做出反应。 当我放开手指时,什么时候跳向上滑动然后跳玩家。我通过我使用的东西来理解这一切 (TouchPhase.Ended)。 我尝试放置 TouchPhase.Moved,但一直在运动之前使用跳跃(TouchPhase.Stationary)。 Mauger 谁遇到过这样的问题? 如果是这样,请告诉我如何操作,即触摸和滑动滑动触摸。

我想到了:

它应该会把你引向正确的方向

    float validInputThresold = 5f;

enum Gestures {
    None,
    Stationary,
    SwipeRight,
    SwipeLeft,
    SwipeUp,
    SwipeDown
}

Gestures currentGesture;

public void Update() {
    currentGesture = Gestures.None;
    HandleTouch(Input.GetTouch(0));
    HandleCharacterMovement(currentGesture);
}

Vector3 originalPosition;
void HandleTouch(Touch touch) {
    if (touch == null) return;



    switch (touch.phase) {
        case TouchPhase.Began:
            originalPosition = touch.position;
            break;
        case TouchPhase.Moved:
            Vector3 delta = touch.position - originalPosition;
            if (delta.magnitude > validInputThresold) Moved(touch, delta);
            break;
        case TouchPhase.Stationary:
            currentGesture = Gestures.Stationary;
            break;
        case TouchPhase.Ended:
        case TouchPhase.Canceled:
            currentGesture = Gestures.None;
            break;
    }
}

public float gestureThresold = 10f;

void Moved(Touch touch, Vector3 delta) {
    if ((Mathf.Abs(delta.x)<=gestureThresold && Mathf.Abs(delta.y)<=gestureThresold)
        || (Mathf.Abs(delta.x)>gestureThresold && Mathf.Abs(delta.y)>gestureThresold) )
            currentGesture = Gestures.Stationary; //IF FINGER MOVED IN DIAGONAL, INVALID STATE FALLSBACK TO STATIONARY
    else if (delta.x > gestureThresold) currentGesture = Gestures.SwipeRight;
    else if (delta.x < -gestureThresold) currentGesture = Gestures.SwipeLeft;
    else if (delta.y > gestureThresold) currentGesture = Gestures.SwipeUp;
    else if (delta.y < -gestureThresold) currentGesture = Gestures.SwipeDown;
}


bool playerIsMoving = false;

// ALL THE ROUTINES HERE SET THE PLAYER IS MOVING FLAG
// TO TRUE AND THEN TO FALSE WHEN THE MOVEMENT IS DONE
void HandleCharacterMovement(Gestures gesture) {
    if (playerIsMoving) return;

    switch (gesture) {

        default:
        case Gestures.None:
        case Gestures.Stationary:
            return;

        case Gestures.SwipeRight:
            StartCoroutine(MovePlayerRight());
            return;
        case Gestures.SwipeLeft:
            StartCoroutine(MovePlayerLeft());
            return;
        case Gestures.SwipeUp:
            StartCoroutine(Jump());
            return;
        case Gestures.SwipeDown:
            StartCoroutine(Crouch());
            return;
    }
}

不确定它是否有效,是用头做的。欢迎指正。

背后的想法:

  • 找到手指
  • 如果有并且它正在移动获取增量
  • 如果 delta 超过一定数量则验证它
  • 如果有效,分配一个您可以跟踪的手势状态
  • 如果玩家尚未移动,则相应地处理玩家移动

这是我的代码:

void Update()
{
#if UNITY_ANDROID || UNITY_IPHONE
    if (Input.touchCount > 0)
    {
        Touch touch = Input.touches[0];

        switch (touch.phase)
        {
            case TouchPhase.Began:
                startPos = touch.position;
                StartCoroutine(Jump());
                break;
            case TouchPhase.Moved:
                isSwipe = true;
                float swipeDistHorizontal = (new Vector3(touch.position.x, 0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
                float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
                if (swipeDistHorizontal > minSwipeDistX)
                {
                    float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
                    if (swipeValue > 0 && !isTouch)//to right swipe
                    {
                        isTouch = true;
                        StartCoroutine(Right());
                    }
                    else if (swipeValue < 0 && !isTouch)//to left swipe
                    {
                        isTouch = true;
                        StartCoroutine(Left());
                    }
                }

                //add swipe to up
                if(swipeDistVertical > minSwipeDistY)
                {
                    float swipeValue = Mathf.Sign(touch.position.y - startPos.y);
                    if(swipeValue > 0 && !isTouch)
                    {
                        isTouch = true;
                        StartCoroutine(Jump2());
                    }
                }
                break;
            case TouchPhase.Ended:
                isSwipe = false;
                isTouch = false;
                break;
        }
    }
    #endif
}
IEnumerator Jump2()
{
    yield return new WaitForSeconds(0.05f);
    if(playerVelocity <= 0.2f)
    {
        Debug.Log("Swipe Up");
    }
}

IEnumerator Jump()
{
    if (!isSwipe)
    {
        yield return new WaitForSeconds(0.05f);
        if (!isSwipe && playerVelocity <= 0.2f)
        {
            Debug.Log("Tap");

        }
        else
        {
            yield break;
        }
    }
    else
    {
        yield break;
    }
}

IEnumerator Right()
{
    Debug.Log("Right");

}

IEnumerator Left()
{
    Debug.Log("Left");

}