Exemplo n.º 1
0
    private void Swipe()
    {
        Vector2 currentSwipe;

        if (Input.touches.Length > 0)
        {
            Touch t = Input.GetTouch(0);
            if (t.phase == TouchPhase.Began)
            {
                //save began touch 2d point
                firstPressPos = new Vector2(t.position.x, t.position.y);
            }
            if (t.phase == TouchPhase.Ended)
            {
                //save ended touch 2d point
                secondPressPos = new Vector2(t.position.x, t.position.y);

                //create vector from the two points
                currentSwipe = new Vector3(secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);

                //normalize the 2d vector
                currentSwipe.Normalize();

                //swipe upwards
                if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
                {
                    Debug.Log("up swipe");
                    _swipeDir = eSwipeDirection.Up;
                    return;
                }
                //swipe down
                if (currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
                {
                    Debug.Log("down swipe");
                    _swipeDir = eSwipeDirection.Down;
                    return;
                }
                //swipe left
                if (currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
                {
                    Debug.Log("left swipe");
                    _swipeDir = eSwipeDirection.Left;
                    return;
                }
                //swipe right
                if (currentSwipe.x > 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f)
                {
                    Debug.Log("right swipe");
                    _swipeDir = eSwipeDirection.Right;
                    return;
                }
            }
        }
        //print("swipe=default");
        _swipeDir = eSwipeDirection.Default;
    }
Exemplo n.º 2
0
    private void HandleTouchGestureInput()
    {
        if(Input.touches.Length == 1)
        {
            //Finger data
            Touch touchedFinger = Input.touches[0];

            switch(touchedFinger.phase)
            {
            case TouchPhase.Began: // stop/start swipe
                m_LastSwipe = InputHandler.eSwipeDirection.None;
                couldBeSwipe = true;
                startPos = touchedFinger.position;
                startTime = Time.time;
                bowlBall.rigidbody.WakeUp();
                for(int i=0; i < pins.Length; i++)
                {
                    pins[i].rigidbody.WakeUp();
                }

                break;
        //			case TouchPhase.Moved: //Detection if not a swipe
        //				if (Mathf.Abs(touchedFinger.position.y - startPos.y) > comfortZone)
        //				{
        //					Debug.Log("Not a swipe. Swipe is " + (int)(Mathf.Abs(touchedFinger.position.y - startPos.y) - comfortZone) + "px outside the comfort zone.");
        //					couldBeSwipe = false;
        //				}
        //
        //				if (Mathf.Abs(touchedFinger.position.x - startPos.x) > comfortZone)
        //				{
        //					Debug.Log("Not a swipe. Swipe is " + (int)(Mathf.Abs(touchedFinger.position.x - startPos.x) - comfortZone) + "px outside the comfort zone.");
        //					couldBeSwipe = false;
        //				}
        //				break;

                //Instantiate(dummy,touchedFinger.position,Quaternion.identity);

            case TouchPhase.Ended: //Swipe detection
                if (couldBeSwipe)
                {
                    bowled = true;
                    float swipeTime = Time.time - startTime;
                    Vector3 swipeDir = touchedFinger.position - startPos;
                    float swipeDist = swipeDir.magnitude;
                    Vector3 swipeDirNormalized = swipeDir.normalized;
                    Vector3 endPos = touchedFinger.position;

        //					Debug.Log("swipeDist : " + swipeDist + " and min swipe dis is : " + minSwipeDist);
        //					Debug.Log("swipeTime : " + swipeTime + " and max swipe time is : " + maxSwipeTime);

                    if ((swipeTime < maxSwipeTime) && (swipeDist > minSwipeDist))
                    {
                        //             // It's a swiiiiiiiiiiiipe!
                        float tolerance = 0.1f;
                        float minSwipeDot = Mathf.Clamp01( 1.0f - tolerance );

                        m_HorizontalSwipeVector = Vector2.right;
                        m_VerticalSwipeVector = Vector2.up;

        //
                        float ang = Vector3.Angle(swipeDir,Vector3.right);
                        float rad = Vector3.Angle(swipeDir,Vector3.right) * Mathf.Deg2Rad;
                        float xForce = Mathf.Cos(rad) * swipeDist*10;
                        float yForce = Mathf.Sin(rad) * swipeDist*10;
                        if(ang > 90.0f)
                            bowlBall.rigidbody.AddForce(-xForce,0,-swipeDist*30);
                        else
                            bowlBall.rigidbody.AddForce(-xForce,0,-swipeDist*30);
                        //Debug.Log("Last swipe : " + m_LastSwipe + " start Pos " + startPos + " end Pos " + endPos + " Angle " + Vector3.Angle(swipeDir,Vector3.right));
                        //Debug.Log("Xforce " + xForce + " yforce " + yForce);
                        debText.text =  "Xforce " + xForce + " yforce " + yForce + " \nstart Pos " + startPos + " \nend Pos " + endPos + " \nAngle " + Vector3.Angle(swipeDir,Vector3.right) + "\nRad " + rad;
                    }
                }
                break;
            default:
                break;
            }
        }
    }
Exemplo n.º 3
0
    private void HandleTouchGestureInput()
    {
        if(Input.touches.Length == 1)
        {
            //Finger data
            Touch touchedFinger = Input.touches[0];

            switch(touchedFinger.phase)
            {
            case TouchPhase.Began: // stop/start swipe
                m_LastSwipe = InputHandler.eSwipeDirection.None;
                couldBeSwipe = true;
                startPos = touchedFinger.position;
                startTime = Time.time;
                break;
            case TouchPhase.Moved: //Detection if not a swipe
                if (Mathf.Abs(touchedFinger.position.y - startPos.y) > comfortZone)
                {
                    Debug.Log("Not a swipe. Swipe is " + (int)(Mathf.Abs(touchedFinger.position.y - startPos.y) - comfortZone) + "px outside the comfort zone.");
                    couldBeSwipe = false;
                }

                if (Mathf.Abs(touchedFinger.position.x - startPos.x) > comfortZone)
                {
                    Debug.Log("Not a swipe. Swipe is " + (int)(Mathf.Abs(touchedFinger.position.x - startPos.x) - comfortZone) + "px outside the comfort zone.");
                    couldBeSwipe = false;
                }
                break;
            case TouchPhase.Ended: //Swipe detection
                if (couldBeSwipe)
                {
                    float swipeTime = Time.time - startTime;
                    Vector3 swipeDir = touchedFinger.position - startPos;
                    float swipeDist = swipeDir.magnitude;
                    Vector3 swipeDirNormalized = swipeDir.normalized;

                    Debug.Log("swipeDist : " + swipeDist + " and min swipe dis is : " + minSwipeDist);
                    Debug.Log("swipeTime : " + swipeTime + " and max swipe time is : " + maxSwipeTime);

                    if ((swipeTime < maxSwipeTime) && (swipeDist > minSwipeDist))
                    {
                        //             // It's a swiiiiiiiiiiiipe!
                        float tolerance = 0.1f;
                        float minSwipeDot = Mathf.Clamp01( 1.0f - tolerance );

        //						switch(Input.deviceOrientation)
        //						{
        //						case DeviceOrientation.FaceDown:
        //						case DeviceOrientation.FaceUp:
        //						case DeviceOrientation.LandscapeLeft:
        //						case DeviceOrientation.LandscapeRight:
        //							m_HorizontalSwipeVector = Vector2.right;
        //							m_VerticalSwipeVector = Vector2.up;
        //							break;
        //						case DeviceOrientation.Portrait:
        //						case DeviceOrientation.PortraitUpsideDown:
        //						case DeviceOrientation.Unknown:
        //							m_HorizontalSwipeVector = Vector2.up;
        //							m_VerticalSwipeVector = -Vector2.right;
        //							break;
        //						default:
        //							break;
        //						}

                        m_HorizontalSwipeVector = Vector2.right;
                        m_VerticalSwipeVector = Vector2.up;

                        if( Vector2.Dot(swipeDirNormalized, m_HorizontalSwipeVector) >= minSwipeDot )
                        {
                            m_LastSwipe = InputHandler.eSwipeDirection.Right;
                        }

                        if( Vector2.Dot(swipeDirNormalized, -m_HorizontalSwipeVector) >= minSwipeDot )
                        {
                            m_LastSwipe = InputHandler.eSwipeDirection.Left;
                        }

                        if( Vector2.Dot(swipeDirNormalized, m_VerticalSwipeVector) >= minSwipeDot )
                        {
                            m_LastSwipe = InputHandler.eSwipeDirection.Up;
                        }

                        if( Vector2.Dot(swipeDirNormalized, -m_VerticalSwipeVector) >= minSwipeDot )
                        {
                            m_LastSwipe = InputHandler.eSwipeDirection.Down;
                        }

                        Debug.Log("Last swipe : " + m_LastSwipe);

                    }
                }
                break;
            default:
                break;
            }
        }
    }
Exemplo n.º 4
0
 void processInput()
 {
     if(m_LastSwipe == eSwipeDirection.Left || m_LastSwipe == eSwipeDirection.Right)
     {
         batScript.batSwinged = true;
         m_LastSwipe = eSwipeDirection.None;
     }
 }