Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DaceProducer_Official · Dec 10, 2014 at 02:26 PM · inputmenuaxisgamepad

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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...

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DaceProducer_Official · Dec 10, 2014 at 04:31 PM 0
Share

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).

avatar image DaceProducer_Official · Dec 10, 2014 at 04:42 PM 0
Share

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

avatar image Sondre-S · Apr 29, 2015 at 06:43 PM 0
Share

@ BedHeadBrum, Thanks man. I had the exact same problem and this solution works great!

avatar image Sondre-S · Apr 30, 2015 at 10:29 AM 0
Share

$$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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Getting R2 and L2 from PS3-Pad to Work on Android 0 Answers

Using Xbox gamepad triggers as keys instead of axis? 1 Answer

Gamepad weird behaviour 1 Answer

Weird Input Manager behaviour 1 Answer

How do you change the joystick axis boundary from a circle to a square? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges