Search Unity

Problems with Touch Screen and Pause Game?

Discussion in '2D' started by Green-Jungle, Jul 14, 2014.

  1. Green-Jungle

    Green-Jungle

    Joined:
    Jun 10, 2014
    Posts:
    79
    Hello everyone.I'm having problem.I'm making a 2d platform game.My character will jump when I touch finger on screen mobile android.
    But when i click on pause button of NGUI then character aslo jump and dialog pause appear .I think because touch screen's still running .
    Now i want to character can't jump when click on pause button that only jump when click on touch screen..how to disable touch screen before click on pause button because it always occur before when click pause button.Please help me.Thank you very much.
     
  2. cbothra

    cbothra

    Joined:
    Mar 14, 2014
    Posts:
    125
    I think you shall Increase the depth of the pause button. That might help.

    Also would like to know on which gameobject you have added the touch event on screen.
     
    Green-Jungle likes this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I'd need to see your "tap to jump" code to be certain, but you probably need to make your character check to see if the tap has been "used" by the pause button before it tells the character to jump.
     
    Green-Jungle likes this.
  4. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    Yeah, i had to do this in my game, i do a raycast to the menu layer for 10 depth, if it hits the pause button i will pause, otherwise the player will do the action.

    Don't know if it's the best way to handle this, because most of the time the player will not touch the pause button and then my input can be some ms delayed every time.
     
    Green-Jungle likes this.
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    If you're doing it correctly there would be no delay at all... Honestly it's tough for me to imagine how a delay would come about unless I explicitly put one in.
     
    Green-Jungle likes this.
  6. Vitor_r

    Vitor_r

    Joined:
    May 23, 2013
    Posts:
    93
    What i mean is that this code:
    Code (CSharp):
    1.  
    2. if (Input.GetTouch(0).phase == TouchPhase.Began){
    3.    ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
    4.    hit = Physics2D.Raycast(ray.origin,ray.direction,10, layerMaskMenu);
    5.    if (hit.transform != null)
    6.      return false;//pause button touched
    7.    return true;//regular input
    8. }
    9.  
    is some ms slower than this:

    Code (CSharp):
    1.  
    2. if (Input.GetTouch(0).phase == TouchPhase.Began){
    3.    return true;//regular input
    4. }
    5.  
     
    Green-Jungle likes this.
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Well, there's an easy solution to that: a touch manager class. Basically, you have one object that does a single raycast. If the raycast hit any GUI objects, call some "OnTapped" function. If not, it calls the "jump" function or whatever.

    Since the engine has to do a raycast at least once no matter how it's written, you may as well arrange the code so that that one raycast is all you ever need per frame.
     
    Green-Jungle likes this.
  8. Green-Jungle

    Green-Jungle

    Joined:
    Jun 10, 2014
    Posts:
    79
    first thing i want to say thank all everyone.this is my code:
    Class TouchScreen attached on Scene
    Code (CSharp):
    1.  PlayerControl player;
    2.     void Start () {
    3.         player = GameObject.Find("Player").GetComponent<PlayerControl>();
    4.     }
    5.  
    6.     void Update()
    7.     {
    8.         if (Input.GetMouseButtonDown(0))
    9.         {
    10.             player.UpdateJumpHandler();  //reference to UpdateJumpHandler function to Jump when touch on screen.
    11.         }
    12.  
    13.      
    14.         //else
    15.         //{
    16.         //    GameControler.buttonPressed = false;
    17.         //}
    18.  
    19.     }
    Then I have a PauseScript class attached on Camera :
    Code (CSharp):
    1.  public void PauseGame()
    2.     {
    3.         if (!isPaused )
    4.         {
    5.           //  Debug.Log("Paused");
    6.             Time.timeScale = 0.0f;
    7.             isPaused = true;
    8.        //     player.enabled = !player.enabled;
    9.         //    EasyTouch.SetEnabled(false);
    10.            // player.jumpPower = 0;
    11.          //   pausePlayer.collider.enabled = false;
    12.         }
    13.         //foreach (GameObject game in gameTag)
    14.         //{
    15.         //    game.gameObject.collider.enabled = false;
    16.         //}
    17.     }
    18.  
    19.  
    20.     public void ResumeGame()
    21.     {
    22.         if (isPaused)
    23.         {
    24.            // Debug.Log("Unpaused");
    25.             Time.timeScale = 1.0f;
    26.             isPaused = false;
    27.             //player.jumpPower = 500f;
    28.  
    29.         }
    I created a button by NGUI called it is Pause and attached a GameControler class :
    Code (CSharp):
    1.  public void ButtonPause()
    2.     {
    3.         // players.jumpPower = 0;
    4.         pause.PauseGame();
    5.         buttonPressed = true;
    6.       //  players.enabled = !players.enabled;
    7.         // touch.enabled = !touch.enabled;
    8.      //   EasyTouch.SetEnabled(false);
    9.         //   Invoke("DelayJump", 1f);
    10.         pauseDialog.GetComponent<TweenPosition>().PlayForward();
    11.     }
    But when I click on Pause Button on Scene then it as image below :

    https://www.dropbox.com/s/ipnnbddren2623a/Untitled.png
    Player jump up when pause beacause touch screen's also running.
    Actually ,I never use Raycast to do this and I don't know use it how.
     
    Last edited: Jul 15, 2014
  9. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    NGUI has a system like the one I've described built in, IIRC. I can't recall what it is called offhand, though.
     
    Green-Jungle likes this.
  10. cbothra

    cbothra

    Joined:
    Mar 14, 2014
    Posts:
    125
    Would like to know from where and how you are calling ButtonPause() method..
     
    Green-Jungle likes this.
  11. Green-Jungle

    Green-Jungle

    Joined:
    Jun 10, 2014
    Posts:
    79
    Cbothra : I create a Game Object Empty on scene then I attached GameControler Class .In Sprite of NGUI I added UIButton Script .Then I drag GameControler class in OnCLick method of Pause Button and choose GameControler.ButtonPause as below imgae :

    https://www.dropbox.com/s/ipnnbddren2623a/Untitled.png
     
  12. cbothra

    cbothra

    Joined:
    Mar 14, 2014
    Posts:
    125
    Then I think you shall add a checking for the pause btn also as mentioned below:

    if(Input.GetMouseButtonDown(0) && !isPaused)

    Also make sure that the isPaused boolean is set before the this condition is triggered in the Update().

    Hope this helps!
     
    Green-Jungle likes this.
  13. Green-Jungle

    Green-Jungle

    Joined:
    Jun 10, 2014
    Posts:
    79
    Thanks all everyone .I did it.I use Easy touch and Raycast.Finally Problem's fixed.Again thank you so much.
     
  14. Marcelo Colombo

    Marcelo Colombo

    Joined:
    Apr 18, 2014
    Posts:
    4
  15. vgvyas98

    vgvyas98

    Joined:
    Feb 2, 2020
    Posts:
    2
    Do you have the file code of how you did with raycast? i am new to unity so i want to see as i have the same problem
     
  16. mos1907

    mos1907

    Joined:
    Jun 7, 2015
    Posts:
    1
    Hello I solved This issue like Below;

    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0))
    2.             {
    3.                 if (EventSystem.current.IsPointerOverGameObject())
    4.                 {
    5.                     PauseGame();
    6.                 }
    7.                 else
    8.                 {
    9.                    \\Your Event
    10.                 }
    11.              
    12.             }