Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Game ignore touches at certain button

Discussion in 'Scripting' started by tomlugin100, Jun 14, 2023.

  1. tomlugin100

    tomlugin100

    Joined:
    May 6, 2023
    Posts:
    51
    Hello everyone and thank you for reading! I'm working on a 2d unity game and I have recently added a pause button for my game. Now, in my game there's a jelly which jumps every time there is a touch. Thus, whenever I press the pause button, the jelly still jumps. How can I fix this?
    One solution I saw from researching what others have done was to create a screen-sized button which would get the touches, and the to use the button as a "shield" stopping touches within its limits. Is there a better way of doing this?
    Humbly,
    Tom
     
  2. tomlugin100

    tomlugin100

    Joined:
    May 6, 2023
    Posts:
    51
    I found a solution and it's so simple I'm surprised I hadn't found it before.
    Code (CSharp):
    1. if (Input.touchCount > 0)
    2.         {
    3.            
    4.             if (PauseButtonScripts.pauseButtonRect.Contains(Input.mousePosition)) {
    5.                 PauseButtonScripts.gamePaused = true;
    6.  
    7.                 Debug.Log("Pause Button Pressed");
    8.             }
    9.  
    10.         }
    I just used the Contains method of the Rect to see if the tap had occured inside it
     
    Chubzdoomer likes this.