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 Phaizran · Apr 04, 2014 at 12:21 AM · c#movementinputposition

Why is Input.GetAxisRaw() not returning whole numbers when using a joystick?

So I need to only receive -1, 0, or 1 as values from the Joystick (using a wired 360 controller at the time). When I use the following code with a keyboard it works exactly as intended, but when I use the thumbstick on the gamepad I get odd movement behaviors. When I tried adding Debug.Log(Input.GetAxisRaw("Vertical"); the console showed it returning values between -0.2 and 0.2. I have no idea what is causing this and while I can think of a way around it it'll take quite a bit of hard coding I'd rather avoid if at all possible.

     float previousAxisStateX;
     float previousAxisStateY;
     float tileSizeX = 2f;
     float tileSizeY = 1f;
     Vector3 transPos;
 
     void Update()
     {
         if (Mathf.Abs(Input.GetAxis("Horizontal")) > Mathf.Abs(Input.GetAxis("Vertical")))
         {
             if (previousAxisStateX == 0)
             {
                 transPos = new Vector3(transform.position.x + (tileSizeX * Input.GetAxisRaw("Horizontal")),transform.position.y,transform.position.z);
                 transform.position = transPos;
             }
         }
         else
         {
             if (previousAxisStateY == 0)
             {
                 transPos = new Vector3(transform.position.x, transform.position.y + (tileSizeY * Input.GetAxisRaw("Vertical")),transform.position.z);
                 transform.position = transPos;
             }
         }
         previousAxisStateX = Input.GetAxis("Horizontal");
         previousAxisStateY = Input.GetAxis("Vertical");
     }
Comment
Add comment · Show 2
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 robertbu · Apr 04, 2014 at 01:50 AM 0
Share

For an analog device, you should be getting values between -1 and 1. You will get a range of values, not just the integers -1, 0, and 1.

avatar image Phaizran · Apr 04, 2014 at 02:08 AM 0
Share

Thanks. I was able to fix it by adding

 int axis = 0;
                 if (Input.GetAxis("Horizontal") > 0)
                     axis = 1;
                 if (Input.GetAxis("Horizontal") < 0)
                     axis = -1;

to my code. As for why it was returning values between -0.2 and 0.2 that was because of my

 if (previousAxisStateX == 0)

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Phaizran · Apr 04, 2014 at 02:47 AM

I was misunderstanding how Input.GetAxisRaw() works. Its doesn't only return whole numbers it merely removes smoothing so an analog stick that can be held at anywhere between -1 and 1 would return values as such. In then end I changed my code to

 float previousAxisStateX;
 float previousAxisStateY;
 float tileSizeX = 2f;
 float tileSizeY = 1f;
 Vector3 transPos;

 

 void Update()
 {
     if (Mathf.Abs(Input.GetAxis("Horizontal")) > Mathf.Abs(Input.GetAxis("Vertical")))
     {
         if (previousAxisStateX == 0)
         {
             int axis = 0;
             if (Input.GetAxis("Horizontal") > 0)
                 axis = 1;
             if (Input.GetAxis("Horizontal") < 0)
                 axis = -1;
             transPos = new Vector3(transform.position.x + (tileSizeX * axis),transform.position.y,transform.position.z);
             transform.position = transPos;
         }
     }
     else
     {
             if (previousAxisStateY == 0)
         {
             int axis = 0;
             if (Input.GetAxis("Vertical") > 0)
                 axis = 1;
             if (Input.GetAxis("Vertical") < 0)
                 axis = -1;
             transPos = new Vector3(transform.position.x, transform.position.y + (tileSizeY * axis),transform.position.z);
             transform.position = transPos;
         }
     }
     previousAxisStateX = Input.GetAxis("Horizontal");
     previousAxisStateY = Input.GetAxis("Vertical");

     
 }
Comment
Add comment · Show 1 · 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 SrHypercube · Dec 16, 2017 at 12:01 PM 0
Share

Thanks for the idea, it was useful to me. I've made my own version of the code:

     Vector3 direction = Vector3.down;
     Vector3 movement = Vector3.zero;
 
     void Update () {
         // input movement
         movement = new Vector3 (
             Input.GetAxisRaw ("Horizontal"),
             Input.GetAxisRaw ("Vertical"),
             0f);
 
         // facing
         if (movement != Vector3.zero)
             CalculateDirection();
     }
 
     void CalculateDirection(){
         int xaxis = 0;
         int yaxis = 0;
 
         if ($$anonymous$$athf.Abs(movement.x) > $$anonymous$$athf.Abs(movement.y))
             xaxis = movement.x > 0 ?  1 : -1;
         else
             yaxis = movement.y > 0 ?  1 : -1;
 
         direction = new Vector3(1f * xaxis, 1f * yaxis, 0f);
     }

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

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

Move a Person to a point / Repeat a function called from another script? 1 Answer

C# sticking to Cube corners position (raycasting AI script) 1 Answer

Problem with player movements 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