예제 #1
0
    public void Update()
    {
#if !UNITY_EDITOR && (UNITY_IPHONE || UNITY_ANDROID || UNITY_BLACKBERRY || UNITY_WP8)
        if (Input.touchCount == 1)
        {
            Touch touch = Input.GetTouch(0);
            if (touch.phase == TouchPhase.Began)
            {
                touchStartPosition = touch.position;
            }
            else if (touch.phase == TouchPhase.Moved && acceptInput)
            {
                Vector2 diff = touch.position - touchStartPosition;
                if (diff.x == 0f)
                {
                    diff.x = 1f; // avoid divide by zero
                }
                float verticalPercent = Mathf.Abs(diff.y / diff.x);

                if (verticalPercent > swipeSensitivty && Mathf.Abs(diff.y) > swipeDistance.y)
                {
                    if (diff.y > 0)
                    {
                        playerController.Jump(false);
                        acceptInput = false;
                    }
                    else if (diff.y < 0)
                    {
                        playerController.Slide();
                        acceptInput = false;
                    }
                    touchStartPosition = touch.position;
                }
                else if (verticalPercent < (1 / swipeSensitivty) && Mathf.Abs(diff.x) > swipeDistance.x)
                {
                    // turn if above a turn, otherwise move horizontally
                    if (swipeToMoveHorizontally)
                    {
                        if (playerController.AbovePlatform(true))
                        {
                            playerController.Turn(diff.x > 0 ? true : false, true);
                        }
                        else if (freeHorizontalMovement)
                        {
                            playerController.MoveHorizontally(diff.x);
                        }
                        else
                        {
                            playerController.ChangeSlots(diff.x > 0 ? true : false);
                        }
                    }
                    else
                    {
                        playerController.Turn(diff.x > 0 ? true : false, true);
                    }
                    acceptInput = false;
                }
            }
            else if (touch.phase == TouchPhase.Stationary)
            {
                acceptInput = true;
            }
            else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
            {
                if ((touch.position - touchStartPosition).sqrMagnitude < 100 && acceptInput)
                {
                    playerController.Attack();
                }
                acceptInput = true;
            }
        }

        if (!swipeToMoveHorizontally)
        {
            CheckHorizontalPosition(Input.acceleration.x);
        }
#else
        if (sameKeyForTurnHorizontalMovement)
        {
            bool hasTurned = false;
            if (Input.GetButtonDown("LeftTurn"))
            {
                hasTurned = playerController.Turn(false, true);
            }
            else if (Input.GetButtonDown("RightTurn"))
            {
                hasTurned = playerController.Turn(true, true);
            }

            // can move horizontally if the player hasn't turned
            if (!hasTurned)
            {
                if (freeHorizontalMovement)
                {
                    if (Input.GetButtonDown("LeftSlot"))
                    {
                        playerController.MoveHorizontally(-horizontalMovementDelta);
                    }
                    else if (Input.GetButtonDown("RightSlot"))
                    {
                        playerController.MoveHorizontally(horizontalMovementDelta);
                    }
                }
                else
                {
                    if (Input.GetButtonDown("LeftSlot"))
                    {
                        playerController.ChangeSlots(false);
                    }
                    else if (Input.GetButtonDown("RightSlot"))
                    {
                        playerController.ChangeSlots(true);
                    }
                }
            }
        }
        else
        {
            if (freeHorizontalMovement)
            {
                if (Input.GetButtonDown("LeftSlot"))
                {
                    playerController.MoveHorizontally(-horizontalMovementDelta);
                }
                else if (Input.GetButtonDown("RightSlot"))
                {
                    playerController.MoveHorizontally(horizontalMovementDelta);
                }
            }
            else
            {
                if (Input.GetButtonDown("LeftSlot"))
                {
                    playerController.ChangeSlots(false);
                }
                else if (Input.GetButtonDown("RightSlot"))
                {
                    playerController.ChangeSlots(true);
                }
            }

            if (Input.GetButtonDown("LeftTurn"))
            {
                playerController.Turn(false, true);
            }
            else if (Input.GetButtonDown("RightTurn"))
            {
                playerController.Turn(true, true);
            }
        }

        if (Input.GetButtonDown("Jump"))
        {
            playerController.Jump(false);
        }
        else if (Input.GetButtonDown("Slide"))
        {
            playerController.Slide();
        }
        else if (Input.GetButtonDown("Attack"))
        {
            playerController.Attack();
        }

        // Move horizontally if the player moves their mouse more than mouseXDeltaValue within a specified amount of time
        if (useMouseToMoveHorizontally)
        {
            if (Input.mousePosition.x != mouseStartPosition)
            {
                if (Time.time - mouseStartTime < 0.5f)
                {
                    float delta = Input.mousePosition.x - mouseStartPosition;
                    bool  reset = false;
                    if (freeHorizontalMovement)
                    {
                        playerController.MoveHorizontally(delta / horizontalMovementSensitivity);
                        reset = true;
                    }
                    else
                    {
                        if (delta > mouseXDeltaValue)
                        {
                            playerController.ChangeSlots(true);
                            reset = true;
                        }
                        else if (delta < -mouseXDeltaValue)
                        {
                            playerController.ChangeSlots(false);
                            reset = true;
                        }
                    }
                    if (reset)
                    {
                        mouseStartTime     = Time.time;
                        mouseStartPosition = Input.mousePosition.x;
                    }
                }
                else
                {
                    mouseStartTime     = Time.time;
                    mouseStartPosition = Input.mousePosition.x;
                }
            }
        }
#endif
    }