Search Unity

Pausing mid game with Touch Input

Discussion in 'Scripting' started by JoeCBS, Sep 2, 2014.

  1. JoeCBS

    JoeCBS

    Joined:
    Feb 25, 2013
    Posts:
    41
    I have a pause button in my mobile game, but I have touch input = 1 to jump. So everytime I touch the screen, whether it's on the pause buton or not, the player will still jump. if I touch the pause button, the play er jumps, but it also pauses the game. is there a way to say, if I touch in the botom left corner of the screen, Pause, not jump? Or isolate the touch by getting it's position?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    The simplest way is probably to expose the rectangle of the pause button so that the jumping code can see it. How you expose this is up to you.

    Then over in the jumping code, instead of just asking "was there a tap?", you will instead ask "was there a tap that was NOT inside the pause button?"

    I use this in my games for cheap-and-cheerful pause buttons.

    Remember that if you're using GUI.Button, the Rect you pass into that has the Y axis reversed from the coordinates you get back from the Input.mousePosition vector, or from the individual Touch objects.

    For instance, if you have:

    Code (csharp):
    1.     Vector2 touchLocation
    2.  
    loaded up with the point onscreen where the user touched, you have to do this first:

    Code (csharp):
    1.  
    2.     Vector2 temp = touchLocation;
    3.     temp.y = Screen.height - temp.y;
    4.  
    then use this:

    Code (csharp):
    1.  
    2.     if (myRectForPauseButton.Contains(temp))
    3.     {
    4.         // do nothing; the pause button will handle it
    5.     }
    6.     else
    7.     {
    8.         // do your jump as before
    9.     }
    10.  
    Otherwise you'll get invalid answers to your .Contains() call.

    Kurt