Esempio n. 1
0
    //Check and set movement and swipes for the ball.
    void CheckMovement()
    {
        //If the ball is allowed to move, multiply the speed we have set to its direction.
        if (isMoving)
        {
            ballRB.velocity = ballSpeed * moveDirection;
        }

        //Create a small collider on the ball to use as a detector for uncolored groundSquares to color the ones we pass on.
        Collider[] ballTouchCollider = Physics.OverlapSphere(transform.position - (Vector3.up / 2), 0.15f);

        int i = 0;

        while (i < ballTouchCollider.Length)
        {
            Ground ground = ballTouchCollider[i].transform.GetComponent <Ground>();
            if (ground && !ground.isColoured)
            {
                ground.ColourChanger(squareColor);
            }
            i++;
        }

        // Checking if we have reached our destination. Then set isMoving to false so we can move the ball again.
        if (collisionPosition != Vector3.zero)
        {
            if (Vector3.Distance(transform.position, collisionPosition) < 1)
            {
                isMoving          = false;
                moveDirection     = Vector3.zero;
                collisionPosition = Vector3.zero;
            }
        }

        //If isMoving is True and we try to move the ball to an another direction, it shouldn't let us, so it returns nothing.
        if (isMoving)
        {
            return;
        }

        //Once the user tabs on the screen to swipe we take their finger's first X and Y position.
        if (Input.GetMouseButton(0))
        {
            swipePositionCurrentFrame = new Vector2(Input.mousePosition.x, Input.mousePosition.y);

            //Did the player's finger move after the initial tab? Then set the destination of mentioned swipe.
            if (swipePositionLastFrame != Vector2.zero)
            {
                currentSwipe = swipePositionCurrentFrame - swipePositionLastFrame;

                //Was it an accidental tab? Then return nothing.
                if (currentSwipe.sqrMagnitude < minSwipeRecognition)
                {
                    return;
                }

                currentSwipe.Normalize();

                //User swiped up or down
                if (currentSwipe.x > -0.5f && currentSwipe.x < 0.5f)
                {
                    if (currentSwipe.y > 0)
                    {
                        SetDestination(Vector3.forward);
                    }
                    else
                    {
                        SetDestination(Vector3.back);
                    }
                }

                //User swiped left or right
                if (currentSwipe.y > -0.5 && currentSwipe.y < 0.5)
                {
                    if (currentSwipe.x > 0)
                    {
                        SetDestination(Vector3.right);
                    }
                    else
                    {
                        SetDestination(Vector3.left);
                    }
                }
            }

            swipePositionLastFrame = swipePositionCurrentFrame;
        }

        //If the player lifts their finger up, don't register anything and set the variables to zero.
        if (Input.GetMouseButtonUp(0))
        {
            swipePositionLastFrame = Vector2.zero;
            currentSwipe           = Vector2.zero;
        }
    }