- Home /
Controller Controlled Menu?
So I am trying to control my project's main menu using a controller, I have the menu reading in the input's of the controller, but it is navigating the menu almost instantly. If you press down on the joystick the menu will go all the way to the bottom, in vise versa for the other direction too. Here is the code:
void Update ()
{
int VerticalInput = (int)Input.GetAxis("Vertical"); // Read in the Vertical input from the controller
// Activate the menu from the title screen, and revert to the title screen
if (!MenuActive && Input.anyKeyDown)
TurnOnMenu();
else if (MenuActive && Input.GetButtonDown("B"))
TurnOnTitle();
// main menu controls
if (MenuActive)
{
if (VerticalInput != 0) // If the player is giving a vertical input
{
StartCoroutine(MenuChange(VerticalInput)); // change the menu
}
MainMenu[Selected].GetComponent<Button>().Select(); // mark the current button selected as selected
}
}
IEnumerator MenuChange(int input)
{
if (input < 0 && Selected < MainMenu.Length - 1)
Selected++;
else if (input > 0 && Selected > 0)
Selected--;
yield return new WaitForSeconds(1f);
StopCoroutine(MenuChange(0));
}
the menu is a series of buttons that are added through the editor into a list of transforms.
Answer by allenallenallen · Jan 02, 2016 at 09:07 AM
I would suggest using a boolean to stop the menu from taking in the commands. In this case, I added a boolean named canInteract.
if (MenuActive)
{
if (VerticalInput != 0 && canInteract) // If the player is giving a vertical input and player can interact with the menu
{
canInteract = false; // Immediately set it to false in case the player triggers another interaction event.
StartCoroutine(MenuChange(VerticalInput)); // change the menu
}
MainMenu[Selected].GetComponent<Button>().Select(); // mark the current button selected as selected
}
Your IEnumerator part:
IEnumerator MenuChange(int input)
{
if (input < 0 && Selected < MainMenu.Length - 1)
Selected++;
else if (input > 0 && Selected > 0)
Selected--;
yield return new WaitForSeconds(1f); // I suggest decreasing the time here. One second for each button is quite a long time, which I'm sure you already know.
canInteract = true; // After the wait is over, the player can interact with the menu again.
}
Answer by meebou · Apr 07, 2019 at 05:06 PM
omg. i lost the whole day experimenting with that. then i threw all code out and it worked suddenly. because unity has it already integrated. :-/ good to know :D
Your answer
![](https://koobas.hobune.stream/wayback/20220612054625im_/https://answers.unity.com/themes/thub/images/avi.jpg)