【问题标题】:Unity3d Touch SwipeUnity3d 触控滑动
【发布时间】:2015-06-30 06:53:12
【问题描述】:

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

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)。 毛格谁遇到过这样的问题? 如果是这样,那么告诉我如何做到这一点是触摸和滑动滑动触摸。

【问题讨论】:

  • 我不得不说我不明白你的问题。请尝试清楚地解释您所期望的行为。
  • 如果我点击屏幕 - 跳跃(一次),如果我滑动(向左或向右) - 滑动(一次)。没用过(TouchPhase.Ended)
  • 请重新表述您问题的下半部分,您无法从中理解您需要什么。

标签: c# ios unity3d game-physics


【解决方案1】:

这是我的代码:

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");

}

【讨论】:

    【解决方案2】:

    我想出了这个:

    它应该让你朝着正确的方向前进

        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 超过一定数量,请验证它
    • 如果有效,则分配您可以跟踪的手势状态
    • 如果玩家尚未移动,则相应地处理玩家移动

    【讨论】:

    • 我需要一键跳转屏幕(需要TouchPhase.Stationary)
    • 你想让角色在玩家的手指接触到碎石但不动的时候跳跃?这相当于按住鼠标。
    • 是的,但是当我移动手指时 - 玩家没有跳跃
    • 我不明白您是在描述当前版本中应该发生的事情还是已经发生的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多