// Update is called once per frame void Update() { //check if there is a touch found if (Input.touchCount > 0) { playerTouch = Input.GetTouch(0); if (playerTouch.phase == TouchPhase.Began) { initialTouchPosition = playerTouch.position; } else if (playerTouch.phase == TouchPhase.Ended || playerTouch.phase == TouchPhase.Canceled) { finalTouchPosition = playerTouch.position; direction = DetectSwipeDirection(initialTouchPosition, finalTouchPosition); if (direction == SwipeDirection.Left) { OnSwipeLeft?.Invoke(); } else if (direction == SwipeDirection.Right) { OnSwipeRight?.Invoke(); } else if (direction == SwipeDirection.Up) { OnSwipeUp?.Invoke(); } else if (direction == SwipeDirection.Down) { OnSwipeDown?.Invoke(); } } } }
public void OnPointerUp(PointerEventData eventData) { Vector2 direction = eventData.position - startDragPos; if (Math.Abs(direction.x) < minDistanceForSwipe && Math.Abs(direction.y) < minDistanceForSwipe) { OnTap?.Invoke(); return; } if (Math.Abs(direction.x) >= Math.Abs(direction.y)) { if (startDragPos.x < eventData.position.x) { OnSwipeRight?.Invoke(); } else { OnSwipeLeft?.Invoke(); } } else { if (startDragPos.y < eventData.position.y) { OnSwipeUp?.Invoke(); } else { OnSwipeDown?.Invoke(); } } startDragPos = Vector2.zero; }
protected override void SwipeDetection() { if (Input.GetMouseButtonDown(0)) { lastPosition = firstPosition; } else if (Input.GetMouseButtonUp(0)) { lastPosition = Input.mousePosition; if (Mathf.Abs(lastPosition.y - firstPosition.y) > dragDistance) { if (lastPosition.y > firstPosition.y) { OnSwipeUp?.Invoke(); } else { OnSwipeDown?.Invoke(); } Debug.Log("OnSwipe"); OnSwipe?.Invoke(); } else { Debug.Log("Tap"); } OnResetPosition(); } }
//Checking and calling respective delegate events void SwipeInputs() { if (swipeManager.SwipeUp && canSwipe) { canSwipe = false; //Calling delegate event if (OnSwipeUp != null) { OnSwipeUp.Invoke(); } } else if (swipeManager.SwipeRight && canSwipe) { canSwipe = false; //Calling delegate event if (OnSwipeRight != null) { OnSwipeRight.Invoke(); } } else if (swipeManager.SwipeDown && canSwipe) { canSwipe = false; //Calling delegate event if (OnSwipeDown != null) { OnSwipeDown.Invoke(); } } else if (swipeManager.SwipeLeft && canSwipe) { canSwipe = false; //Calling delegate event if (OnSwipeLeft != null) { OnSwipeLeft.Invoke(); } } else if (swipeManager.Tap) { //Calling delegate event if (OnSingleTap != null) { OnSingleTap.Invoke(); } } else if (swipeManager.DoubleTap) { //Calling delegate event if (OnDoubleTap != null) { OnDoubleTap.Invoke(); } } }
public void SetPause(bool set = true) { isPaused = set; if (isPaused == true) { OnSwipeDown.Invoke(); } }
protected override void SwipeDetection() { if (Input.touchCount == 1) { Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began) { firstPosition = touch.position; lastPosition = touch.position; } else if (touch.phase == TouchPhase.Moved) { lastPosition = touch.position; isTouchMoved = true; } else if (touch.phase == TouchPhase.Ended) { firstPosition = touch.position; if (Mathf.Abs(lastPosition.y - firstPosition.y) > dragDistance) { if (isTouchMoved) { if (lastPosition.y > firstPosition.y) { OnSwipeUp?.Invoke(); } else { OnSwipeDown?.Invoke(); } OnSwipe?.Invoke(); isTouchMoved = false; } } } else { Debug.Log("Tap"); } OnResetPosition(); } }
public void CompareByNormalizedFloat() { if (offSet.x > swipeDetectionLimitLeftRight && enableHorizontalSwipe) { OnSwipeRight?.Invoke(this, EventArgs.Empty); return; } if (offSet.x < swipeDetectionLimitLeftRight && enableHorizontalSwipe) { OnSwipeLeft?.Invoke(this, EventArgs.Empty); return; } if (offSet.y > swipeDetectionLimitUpDown && enableVerticalSwipe) { OnSwipeUp?.Invoke(this, EventArgs.Empty); return; } if (offSet.y < swipeDetectionLimitUpDown && enableVerticalSwipe) { OnSwipeDown?.Invoke(this, EventArgs.Empty); return; } OnSwipeCancel?.Invoke(this, EventArgs.Empty); }
public void Update() { //Dont check inputs when dead or not running if (!manager.isGameStarted || manager.isDead) { return; } //Reset swipe status to prevent multiple event triggers swipedRight = false; swipedLeft = false; swipedUp = false; swipedDown = false; if (Input.touches.Length > 0) { t = Input.GetTouch(0); if (t.phase == TouchPhase.Began) { //Look for single taps when in a boss fight if (t.position.y > Screen.height / 2f && manager.bossActive) { OnSingleTap.Invoke(); return; } startPos = new Vector2(t.position.x / Screen.width, t.position.y / Screen.width); tapCount++; } else if (t.phase == TouchPhase.Moved && !swiped) { endPos = new Vector2(t.position.x / Screen.width, t.position.y / Screen.width); swipe = new Vector2(endPos.x - startPos.x, endPos.y - startPos.y); // Too short swipe if (swipe.magnitude < MIN_SWIPE_DISTANCE) { return; } swiped = true; if (Mathf.Abs(swipe.x) > Mathf.Abs(swipe.y)) { // Horizontal swipe if (swipe.x > 0) { swipedRight = true; OnSwipeRight.Invoke(); } else { swipedLeft = true; OnSwipeLeft.Invoke(); } } else { // Vertical swipe if (swipe.y > 0) { swipedUp = true; OnSwipeUp.Invoke(); } else { swipedDown = true; OnSwipeDown.Invoke(); } } } else if (t.phase == TouchPhase.Ended) { swiped = false; } } //Too long for double tap if (doubleTapTimer > MAX_DOUBLE_TAP_TIME) { doubleTapTimer = 0f; tapCount = 0; } //Double tap else if (tapCount >= 2) { doubleTapTimer = 0.0f; tapCount = 0; OnDoubleTap.Invoke(); } //Increase double tap timer else if (tapCount > 0) { doubleTapTimer += Time.deltaTime; } //If debugging, look for keyboard inputs if (debugWithArrowKeys) { if (Input.GetKeyDown(KeyCode.RightArrow)) { OnSwipeRight.Invoke(); } if (Input.GetKeyDown(KeyCode.LeftArrow)) { OnSwipeLeft.Invoke(); } if (Input.GetKeyDown(KeyCode.UpArrow)) { OnSwipeUp.Invoke(); } if (Input.GetKeyDown(KeyCode.DownArrow)) { OnSwipeDown.Invoke(); } if (Input.GetKeyDown(KeyCode.E)) { OnSingleTap.Invoke(); } if (Input.GetKeyDown(KeyCode.Space)) { OnDoubleTap.Invoke(); } } }
private void Update() { //Check Device Orientation Maybe use DougMcFarlane Device Change class from https://forum.unity.com/threads/device-screen-rotation-event.118638/ if (Input.deviceOrientation == DeviceOrientation.Portrait) { //Run all methods subscribed to event OnOrientationPortrait?.Invoke(); } else if (Input.deviceOrientation == DeviceOrientation.LandscapeLeft || Input.deviceOrientation == DeviceOrientation.LandscapeRight) { OnOrientationLandscape?.Invoke(); } //check if there is a touch found if (Input.touchCount > 0) { initialPlayerTouch = Input.GetTouch(0); //Test to see if this will cause any issues. //OnTouchDrag?.Invoke(initialPlayerTouch); touchTimer += Time.deltaTime; //check if we are touching a UI element and if we are dont do any input commands !!!MOVE INTO TOUCHPHASE.ENDED IF if (IsTouchOverUIElement(initialPlayerTouch) == false) { if (initialPlayerTouch.phase == TouchPhase.Began) { initialTouchPosition = initialPlayerTouch.position; } else if (initialPlayerTouch.phase == TouchPhase.Moved) { OnTouchDrag?.Invoke(initialPlayerTouch); } else if (initialPlayerTouch.phase == TouchPhase.Ended) { finalTouchPosition = initialPlayerTouch.position; touchTimer = touchTimer; direction = DetectSwipeDirection(initialTouchPosition, finalTouchPosition); if (direction == SwipeDirection.Left) { OnSwipeLeft?.Invoke(); } else if (direction == SwipeDirection.Right) { OnSwipeRight?.Invoke(); } else if (direction == SwipeDirection.Up) { OnSwipeUp?.Invoke(); } else if (direction == SwipeDirection.Down) { OnSwipeDown?.Invoke(); } else if (direction == SwipeDirection.None) { if (touchTimer < tapTime) { StartCoroutine("DoubleTap"); } else { OnSingleTouchHeld?.Invoke(initialPlayerTouch); } } touchTimer = 0f; } } } }
public void DoUpdate() { if (Input.touchCount == 1) // user is touching the screen with a single touch { var touch = Input.GetTouch(0); // get the touch switch (touch.phase) { //check for the first touch case TouchPhase.Began: _fp = touch.position; _lp = touch.position; break; // update the last position based on where they moved case TouchPhase.Moved: _lp = touch.position; break; //check if the finger is removed from the screen case TouchPhase.Ended: { _lp = touch.position; //last touch position. Ommitted if you use list //Check if drag distance is greater than 20% of the screen height if (Mathf.Abs(_lp.x - _fp.x) > _dragDistance || Mathf.Abs(_lp.y - _fp.y) > _dragDistance) { //It's a drag //check if the drag is vertical or horizontal if (Mathf.Abs(_lp.x - _fp.x) > Mathf.Abs(_lp.y - _fp.y)) { //If the horizontal movement is greater than the vertical movement... if ((_lp.x > _fp.x)) //If the movement was to the right) { //Right swipe OnSwipeRight?.Invoke(); } else { OnSwipeLeft?.Invoke(); //Left swipe } } else { //the vertical movement is greater than the horizontal movement if (_lp.y > _fp.y) //If the movement was up { //Up swipe OnSwipeUP?.Invoke(); } else { OnSwipeDown?.Invoke(); //Down swipe } } } else { OnTap?.Invoke(); //It's a tap as the drag distance is less than 20% of the screen height Debug.Log("Tap"); } break; } } } }
public static void RaiseSwipeDown(object sender) { OnSwipeDown?.Invoke(sender, EventArgs.Empty); }