private void TouchMovement() { if (Input.touchCount >= 1) { Touch[] touches = Input.touches; Touch touch = Input.touches[0]; foreach (Touch t in touches) //Iterate through all touches { if (t.position.x < Screen.width / 2) //If a touch is on the left side { touch = t; //Use that touch as the main one break; } } if (touch.position.x < Screen.width / 2) //If touch is on the left side of the screen { p.MovementTouch = touch; firstTimeSwipe = false; if (touch.phase == TouchPhase.Began) { //OLD MOVEMENT initialPosMovement = touch.position; initialPressPos[0] = touch.position; endPressPos[0] = touch.position; swipeDirection[0] = Vector2.zero; UI_MovementManager.Instance.TouchInitialize(initialPressPos[0]); } if (touch.phase == TouchPhase.Moved) { if (initialPosMovement == Vector2.zero) { return; } //NEW MOVEMENT /*Vector2 deltaPos = touch.deltaPosition; * * if (Vector2.Distance(deltaPos, Vector2.zero) > Vector2.Distance(greatestDeltaPos, Vector2.zero)) * greatestDeltaPos = deltaPos; * * * Vector2 up = transform.up; * * //if (deltaPos == Vector2.zero) //If there was no finger movement * if (deltaPos == Vector2.zero) //If there was no finger movement * return; //Do nothing (stop) * * //If the change in position of the touch is greater than a threshold amount then calculate the movement direction * //This removes the hyper sensitivity to change in direction which could cause unintentional and erratic direction changes * //Funny fact: setting a really low delta pos threshold resulted in jitery fliping when firing and walking at once... which sucked * if (deltaPos.x > deltaPosThreshold || deltaPos.y > deltaPosThreshold) { * //Calculate the movement direction * * if (up == Vector2.up || up == Vector2.down) * moveDir = Accessories.ClosestDirection(deltaPos, DirectionRoundTypes.HORIZONTAL); * else if (up == Vector2.right || up == Vector2.left) * moveDir = Accessories.ClosestDirection(deltaPos, DirectionRoundTypes.VERTICAL); * * moveCharacter(determineMovement(moveDir)); * * lastCalculatedPos = touch.position; //Record the last finger position that the player's direction was calculated * * lastDeltaPos = deltaPos; //Update the last pos (this is only referenced again on the next frame) * } * //If the finger has strayed too far from the original turn point position * //Can be done by abusing the deltaPosThreshold mechanic by moving finger really slow * //Which causes it directional change not to be calculated * //else if (Mathf.Abs(Vector2.Distance(lastCalculatedPos, touch.position)) > fingerStrayThreshold) { * * else if (Mathf.Abs(lastCalculatedPos.x - touch.position.x) / Screen.width * 100 > fingerStrayThreshold || Mathf.Abs(lastCalculatedPos.y - touch.position.y) / Screen.height * 100 > fingerStrayThreshold) { * //Force a movement direction recalculation * if (up == Vector2.up || up == Vector2.down) * moveDir = Accessories.ClosestDirection(deltaPos, DirectionRoundTypes.HORIZONTAL); * else if (up == Vector2.right || up == Vector2.left) * moveDir = Accessories.ClosestDirection(deltaPos, DirectionRoundTypes.VERTICAL); * * moveCharacter(determineMovement(moveDir)); * * lastCalculatedPos = touch.position; //Record the last finger position that the player's direction was calculated * * lastDeltaPos = deltaPos; //Update the last pos (this is only referenced again on the next frame) * } * else { //If nothing is trigger to this point then continue moving in the last valid known direction * if (up == Vector2.up || up == Vector2.down) * moveDir = Accessories.ClosestDirection(lastDeltaPos, DirectionRoundTypes.HORIZONTAL); * else if (up == Vector2.right || up == Vector2.left) * moveDir = Accessories.ClosestDirection(lastDeltaPos, DirectionRoundTypes.VERTICAL); * * moveCharacter(determineMovement(moveDir)); * } */ //OLD MOVEMENT endPressPos[0] = touch.position; swipeVector[0] = new Vector2(endPressPos[0].x - initialPressPos[0].x, endPressPos[0].y - initialPressPos[0].y); //distanceVector[0] = new Vector2(Mathf.Abs((swipeVector[0].x / Screen.width) * 100), Mathf.Abs((swipeVector[0].y / Screen.height) * 100)); Vector2 distanceVec = new Vector2(Mathf.Abs(swipeVector[0].x), Mathf.Abs(swipeVector[0].y)); //if (distanceVector[0].x > movementThreshold || distanceVector[0].y > movementThreshold) { //If the swipe is big enough... if (distanceVec.x > movementThreshold || distanceVec.y > movementThreshold) { //swipeDirection[0] = Accessories.ClosestDirection(swipeVector[0], DirectionRoundTypes.ALL); //Determine the overall direction of the swipe //swipeDirection[0] = Accessories.ClosestDirection(swipeVector[0], DirectionRoundTypes.VERTICAL); //Determine the overall direction of the swipe swipeDirection[0] = Accessories.ClosestDirection(swipeVector[0], DirectionRoundTypes.HORIZONTAL); //Determine the overall direction of the swipe moveCharacter(determineMovement(swipeDirection[0]), true); } else //If the movement isn't enough { swipeDirection[0] = Vector2.zero; //Set swipe direction to nothing } UI_MovementManager.Instance.TouchUpdate(endPressPos[0]); } if (touch.phase == TouchPhase.Stationary) { if (initialPosMovement == Vector2.zero) { return; } //NEW MOVEMENT /*Vector2 deltaPos = touch.deltaPosition; * * if (Vector2.Distance(deltaPos, Vector2.zero) > Vector2.Distance(greatestDeltaPos, Vector2.zero)) * greatestDeltaPos = deltaPos; * * if (greatestDeltaPos.x > 0.5f || greatestDeltaPos.y > 0.5f) * moveCharacter(determineMovement(moveDir)); */ moveCharacter(determineMovement(swipeDirection[0]), true); //OLD MOVEMENT UI_MovementManager.Instance.TouchUpdate(endPressPos[0]); } if (touch.phase == TouchPhase.Ended) { //NEW MOVEMENT //greatestDeltaPos = Vector2.zero; //Reset //OLD MOVEMENT initialPosMovement = Vector2.zero; initialPressPos[0] = Vector2.zero; endPressPos[0] = Vector2.zero; UI_MovementManager.Instance.TouchEnd(); anim.SetBool("walk", false); } } else { if (firstTimeSwipe == false) { firstTimeSwipe = true; initialPressPos[0] = Vector2.zero; endPressPos[0] = Vector2.zero; UI_MovementManager.Instance.TouchEnd(); anim.SetBool("walk", false); } } } }
private void TouchGravityFlip() { if (Input.touchCount > 0) { Touch touch1 = Input.touches[0]; if (touch1.phase == TouchPhase.Began) { beginningTime[0] = Time.time; initialPressPos[0] = touch1.position; endPressPos[0] = touch1.position; } } if (Input.touchCount == 2) { Touch[] touches = { Input.touches[0], Input.touches[1] }; if (touches[1].phase == TouchPhase.Began) { beginningTime[1] = Time.time; initialPressPos[1] = touches[1].position; endPressPos[1] = touches[1].position; if (beginningTime[1] - beginningTime[0] < timeDifferenceThreshold) //If start times are small enough { p.DoubleSwipe = true; } else //If not { p.DoubleSwipe = false; } } if (!p.DoubleSwipe) //If the swipe doesn't count { return; //Stop from progressing any futher } if (touches[0].phase == TouchPhase.Moved || touches[1].phase == TouchPhase.Moved) //If one of the touches moves { for (int i = 0; i < 2; i++) { endPressPos[i] = new Vector2(touches[i].position.x, touches[i].position.y); swipeVector[i] = new Vector2(endPressPos[i].x - initialPressPos[i].x, endPressPos[i].y - initialPressPos[i].y); Vector2 distanceVec = new Vector2(Mathf.Abs(swipeVector[0].x), Mathf.Abs(swipeVector[0].y)); if (distanceVec.x > swipeThreshold || distanceVec.y > swipeThreshold) { swipeDirection[i] = Accessories.ClosestDirection(swipeVector[i], DirectionRoundTypes.VERTICAL); //Determine the overall direction of the swipe } } } if (touches[0].phase == TouchPhase.Ended || touches[1].phase == TouchPhase.Ended) //If one of them ends { for (int i = 0; i < 2; i++) //Reset the values { initialPressPos[i] = Vector2.zero; endPressPos[i] = Vector2.zero; } } } if (swipeDirection[0] == Vector2.zero || swipeDirection[1] == Vector2.zero) { return; } if (swipeDirection[0] == swipeDirection[1]) { changeGravity(swipeDirection[0]); } }
private void TouchJump() { if (Input.touchCount >= 1) { Touch[] touches = Input.touches; Touch touch = Input.touches[0]; foreach (Touch t in touches) //Iterate through all touches { if (t.position.x > Screen.width / 2) //If a touch is on the right side { touch = t; break; } } if (touch.position.x > Screen.width / 2) //If touch is on the right side of the screen { if (touch.phase == TouchPhase.Began) { initialPressPos = touch.position; endPressPos = touch.position; firstJumpCall = true; } if (touch.phase == TouchPhase.Moved) { if (initialPressPos == Vector2.zero) { return; } endPressPos = new Vector2(touch.position.x, touch.position.y); swipeVector = new Vector2(endPressPos.x - initialPressPos.x, endPressPos.y - initialPressPos.y); distanceVector = new Vector2(Mathf.Abs((swipeVector.x / Screen.width) * 100), Mathf.Abs((swipeVector.y / Screen.height) * 100)); if (Input.touchCount == 2) { //If both swipes are moving in the same direction and the timing could be a double swipe if (Accessories.ClosestDirection(touches[1].deltaPosition, DirectionRoundTypes.ALL) == Accessories.ClosestDirection(touches[0].deltaPosition, DirectionRoundTypes.ALL) && p.DoubleSwipe) { return; //Don't attempt to jump } } if (distanceVector.x > swipeThreshold || distanceVector.y > swipeThreshold) //If the swipe is big enough... { Vector3 up = Accessories.ClosestDirection(transform.up, DirectionRoundTypes.ALL); if (up == Vector3.up || up == Vector3.down) //If the player's gravity is on the vertical axis { swipeDirection = Accessories.ClosestDirection(swipeVector, DirectionRoundTypes.VERTICAL); //Determine the overall direction of the swipe } else //If the player's gravity is on the horizontal axis { swipeDirection = Accessories.ClosestDirection(swipeVector, DirectionRoundTypes.HORIZONTAL); //Determine the overall direction of the swipe } if (swipeDirection.x == up.x && swipeDirection.y == up.y) { if (firstJumpCall) { firstJumpCall = false; Jump(); //Try to jump using a specific jump (needed b/c it runs more than once) } } //Reset the touch initialPressPos = touch.position; endPressPos = touch.position; } } if (touch.phase == TouchPhase.Ended) { //Reset variables initialPressPos = Vector2.zero; endPressPos = Vector2.zero; } } } }