Touch sides of tablet, moves that direction

| | August 8, 2015

I am making a game where the player flies a balloon through a cavelike structure.

Balloon flies through maze

I am having trouble making him fly correctly when I try it out on my android tablet, seeing as it requires different input to move.

enter image description here

How can I make the player move towards the touched side? By creating a ray? through touched objects? :)

2 Responses to “Touch sides of tablet, moves that direction”

  1. Following along with the other suggestions, you can use the screen width to determine the left or right side of the screen. I might also suggest using a dead spot around the balloon. Precision touching can be difficult. if you split the screen on a single column of pixels, touches near this divide might get registered on the wrong side of the line that the player wants.

    This bit of pseudo code assumes that the balloon stays in the center of the screen and the background moves.

    var leftBounds  : float = screenWidth/2 - balloonWidth/2;
    var rightBounds  : float = screenWidth/2 + balloonWidth/2;
    if (Input.touchCount > 0) {
        var touchPositionX : float = Input.getTouch(0).position.x;
        if (touchPositionX <= leftBounds) {
             rigidbody2d.AddForce(-vector2.right*5);
        }
        if (touchPositionX >= rightBounds) {
            rigidbody2d.AddForce(vector2.right*5);
        }
    }
    

    If the balloon does not stay in the center of the screen, then the leftBounds and rightBounds will have to be calculated off of the balloons current position.

  2. Using two different inputs (one finger and two fingers) for a same action (add a force) can be disturbing.

    I would suggest you to only use one finger and orient the force accordingly to the touch position on screen.

    As you seem to use Unity, you can easily get the touch position thanks to Input.mousePosition. Combined with Screen.width you will know if player has touched left or right side of the screen.

    I don’t know much about the actual gameplay of your game but with such technique it’s easy to determine the actual force applied accordingly to the finger position… you could says something like “the closest of the border your tap is the biggest the applied force is”

    I hope it helps.

Leave a Reply