- Home /
horizontal menu + gamepad
I'm making a horizontal selection menu and I would like the player to be able to select a spell from the menu using a gamepad.
I put this piece of code in the Update()
float moveHorizontal = Input.GetAxis ("Horizontal_P1");
Debug.Log ("moveHorizontal " + moveHorizontal);
if(Input.GetKeyDown (KeyCode.Q) || moveHorizontal == -1)
{
if(spellSelected > 0)
{
spellSelected --; // this changes the spell in mySpellSelected array
boxPosA --; // a "frame" GUIbox around the selected spell
}
}
if(Input.GetKeyDown (KeyCode.W) || moveHorizontal == 1)
{
if(spellSelected < 3)
{
spellSelected ++; // this changes the spell in mySpellSelected array
boxPosA ++; // a "frame" GUIbox around the selected spell
}
}
The problem is the "frame" GUIbox jumps from one side of the menu to the other very quickly, because it's updated every frame - that much I understand. Any ideas how to make the GetAxis behave like GetButtonDown instead of GetButton?
Answer by SethSR · Jun 19, 2014 at 07:32 AM
Use something like:
if (!received_input)
{
if (moveHorizontal < 0) moveSelectedLeft();
if (moveHorizontal > 0) moveSelectedRight();
received_input = true;
}
else if (moveHorizontal == 0)
received_input = false;
received_input
ensures the player has to release the stick before moving the selection again. If they only need to release part-way, you could do:
...
else if (Mathf.Abs(moveHorizontal) < release_threshold)
received_input = false;
You could also use a timer to switch between "step-selection" (like above), and "rapid-selection" (like you already have), if the menus are fairly large, to make it easier for the player to cycle though the items.
Your answer
Follow this Question
Related Questions
if (Input.GetAxisRaw("horizontalP1") always does what 0 axis has assigned 0 Answers
Double tap mechanics and axis inputs 3 Answers
Scale gameObject 1 Answer
Mapping multiple controllers 1 Answer
link gamepad to player 0 Answers