- Home /
Input.GetAxisRaw
Hello, I'm working on a main menu for my game. After setting up the Input Manager for use with a game pad, I wrote a basic script which should allow me to navigate through the main menu. The problem is, it's not working as I intended. When I move the control stick up or down to scroll through the menu buttons, it starts doing so very rapidly on it's own, and I have no control whatsoever. Here's the script I wrote:
 #pragma strict
 
 // The menu buttons
 var thePlay : GameObject;
 var theTutorial : GameObject;
 var theOptions : GameObject;
 var theCredits : GameObject;
 var theExit : GameObject;
 
 // The position of the button in the menu
 var menuPosition : int;
 
 // The selected button
 var theSelection : GameObject;
 
 function Start ()
 {
     menuPosition = 0;
     theSelection = thePlay;
     theSelection.renderer.material.color = Color.red;
 }
 
 function Update ()
 {
     if (position == 0)
     {
         if (Input.GetAxisRaw ("Gamepad Y") < 0)
         {
             menuPosition = 1;
             theSelection.renderer.material.color = Color.white;
             theSelection = theTutorial;
             theSelection.renderer.material.color = Color.red;
         }
         else if (Input.GetAxisRaw ("Gamepad Y") > 0)
         {
             menuPosition = 4;
             theSelection.renderer.material.color = Color.white;
             theSelection = theExit;
             theSelection.renderer.material.color = Color.red;
         }
     }
     else if...
 
     //And so on for all other menu positions
     }
     Debug.Log (Input.GetAxisRaw ("Gamepad Y");
 }
Where is the problem? I've been striving to get this script working, three days already, and still can't figure out what's wrong. Could someone kindly help me?
Answer by BedHeadBrum · Dec 10, 2014 at 02:58 PM
Maybe try decreasing the sensitivity by using greater / lesser values in your conditions. This will stop small movements from been used to navigate the menu.
if (Input.GetAxisRaw ("Gamepad Y") < 0.2)
I imagine it is scrolling through uncontrollably because you have no forced delay between menu movements. This is a simple but not very elegant way to do this. It will force a delay of 0.2 seconds between menu movements.
 if (Input.GetAxisRaw ("Gamepad Y") < 0.2 && lastMenuMovement < Time.time - 0.2)
     {
              lastMenuMovement = Time.time;
              menuPosition = 1;
              theSelection.renderer.material.color = Color.white;
              theSelection = theTutorial;
              theSelection.renderer.material.color = Color.red;
     }
 
Just store the lastMenuMovement float in the class somewhere. Hope this helps...
Thanks for the response. Fiddling with the condition value seems to make the situation a little bit better, but the movement still seems way too fast. Adding the last$$anonymous$$enu$$anonymous$$ovement variable in the class doesn't seem to do the trick either, it doesn't allow me to scroll (because Time.time is always going to be > Time.Time - 0.2).
Oh, so I had to simply set the last$$anonymous$$enu$$anonymous$$ovement variable as a float WITHOUT assigning it equals to Time.time, no? At least not before the condition. But it still isn't working
@ BedHeadBrum, Thanks man. I had the exact same problem and this solution works great!
$$anonymous$$any people(me, included) need this for a pause menu, and you most likely wanna set time scale to 0, therefore Time.time would not work and should really be changed to Time.realtimeSinceStartup.
 float last$$anonymous$$enu$$anonymous$$ovement;
 
 start(){
 
 }
 
 update(){
 
 if (Input.GetAxisRaw ("Gamepad Y")> 0 && last$$anonymous$$enu$$anonymous$$ovement < Time.realtimeSinceStartup - 0.2)
      {
               last$$anonymous$$enu$$anonymous$$ovement = Time.realtimeSinceStartup;
               menuPosition = 1;
               theSelection.renderer.material.color = Color.white;
               theSelection = theTutorial;
               theSelection.renderer.material.color = Color.red;
      }
 
 
 }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                