- Home /
Navigating menus using Joystick
I have a menu system in my game. The menu works with arrow keys on the keyboard and with the joystick/d-pad on an xbox controller. Since the joystick uses an axis I can not use Input.GetButtonDown, I have to use Input.GetAxis("Vertical"). I have Vertical mapped to the xbox joystick, the d-pad, and the arrow keys to cover both in 1 command.
The problem I was having then, was when I would hold down the joystick it would rapidly move ( I mean 10/second ) since this is in the Update() function.
As an attempt to solve the problem I have added a boolean value. When 0 < input > 0 it calls the MoveUp or MoveDown function appropriately. Within those two functions I have a value that turns false, and in MoveUp/Down function if this value is false it does not continue.
(I also have a counter which is an int. It only goes from 1 to 3 each representing an item in the menu, I only have 3 at this time.
Please I am open to any improvements to this code.)
function Update(){
var inputY = Input.GetAxis("Vertical");
if (inputY > 0){
MoveUp();
} else{
canMoveUp = true;
}
if (inputY < 0){
MoveDown();
} else{
canMoveDown = true;
}
}
function MoveUp(){
if (canMoveUp == true){
if (count == 3){
count = 2;
canMoveUp = false;
} else if (count == 2){
count = 1;
canMoveUp = false;
}
if (count == 1){
count = 1;
canMoveUp = false;
}
}
}
function MoveDown(){
if (canMoveDown == true){
if (count == 1){
count = 2;
canMoveDown = false;
} else if (count == 2){
count = 3;
canMoveDown = false;
}
if (count == 3){
count = 3;
canMoveDown = false;
}
}
}
Now, I have a problem with this code. If I am holding down the down key (or holding the joystick down), I'm essentially calling this MoveDown function every frame. I believe this causes some lag on the system because if I let go and try to move down again rapidly, there is some delay (1-2secs). It is a minor bug, but since my menu systems are working 100% at this point. I'd like to refine and fix this bug.
Your answer
Follow this Question
Related Questions
How do you make a Main Menu that only takes Joystick input and is not affected by the Cursor 2 Answers
How do I 360 A button working 0 Answers
How can i move an object in screen space with xbox joystick 1 Answer
How to use the XBox 360 Controller D-Pad (PC) ? 6 Answers
Tattie Bogle doesn't distinguish between multiple Xbox 360 controllers for Mac OS X El Capitan 0 Answers