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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Using same Gamepad button for two actions, in game and menu selection

Discussion in 'Scripting' started by The-NightflyAZ, Sep 20, 2020.

  1. The-NightflyAZ

    The-NightflyAZ

    Joined:
    Apr 27, 2015
    Posts:
    17
    Hey everyone.

    To get right into things, I'm working on a game where the "A" button on an XBox One controller has two actions.

    1) During gameplay, the "A" button makes the character jump but...

    2) ...in a menu, it's the select button.

    What happens is when I click out of a menu, my character jumps and i don't want that. I've tried adding a boolean to check whether or not a menu is open and put that around the jump code like,

    Code (CSharp):
    1.             if (gameManager.gameIsPaused == false)
    2.             {
    3.                 if (Input.GetButtonDown("Jump"))
    4.                 {
    5.                     Jump();
    6.                 }
    7.             }
    but it isn't doing anything. It's almost like it button works on the menu item and then the menu closes, the boolean switches to the gameIsPaused ==false and then the engine reads it as, "Oh, gameIsPaused, so I'll go ahead and jump too."

    I can think of a fairly inelegant solution but I know there has to be a simple, clean way to do this.

    If you need additional clarification I can elaborate.

    Thanks!
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    Try adding Debug.Logs() all over your code to see what exactly is happening. Is it possible that you are perhaps unpausing the game mere moments before the jump code is even called?
     
  3. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Well you can resume the game at a coroutine with some delay in order to avoid that... or just use Input.GetButtonUp() for resume the game since this always will be proc after GetButtonDown. Just lazy ideas but it should work.
     
  4. The-NightflyAZ

    The-NightflyAZ

    Joined:
    Apr 27, 2015
    Posts:
    17
    Thank you so much, rubcc95. I'll give those suggestions a try today.
     
  5. The-NightflyAZ

    The-NightflyAZ

    Joined:
    Apr 27, 2015
    Posts:
    17
    I'll do that to double check.