Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 StevenUnu · Sep 09, 2017 at 10:15 AM · floatinput.getaxiscalculate

How does Input.GetAxis work with a keyboard?

I have a simple character controller. This is a snippet from my code:

 Rigidbody2D rb ; // in Start this gets gameObject.GetComponent<Rigidbody2D>()
 float maxSpeed = 5f ;
 ...
 void Update() {
 ...
 float move = Input.GetAxis("Horizontal") ;
 print(move) ;
 if(move != 0)
     rb.velocity = new Vector2(move * maxSpeed, rb.velocity.y) ;
 }
 

I can see that when I don't press neither the Left Arrow Key (the negative axis key) nor the Right Arrow Key (the possitive axis key), Input.GetAxis returns 0, when I press the Left Arrow key, it starts returning a value that is between (-1, 0) and after about 1s of pressing it returns -1, same with the Right Arrow key but between (0, +1). I found on a forum that with a keyboard Input.GetAxis always returns -1, 0 and 1 but I get values between. Is this normal? How are this values calculated? Are they related to how long did the user press the key or how hard did he press it?

Target

As many people, I am disapointed by Unity's Input Manager and I'm writting my own Custom Input Manger. I need to know how can I calculate theese intermediate values in my public static float GetAxis(string axisName)

Update (Clarification)

I'm writing my custom Input Manager because I need to be able to change axes at runtime.

Comment
Add comment · Show 1
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 StevenUnu · Sep 09, 2017 at 01:45 PM 0
Share

Someone some help?

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TheSOULDev · Sep 09, 2017 at 02:12 PM

If you need straight up full values, use GetAxisRaw. GetAxis by itself lerps the values based on the settings in the input manager, while GetAxisRaw returns the raw version of input, without any lerping. In essence, it simulates the input of a virtual joystick, where it is impossible to pull the joystick fast enough for it to instantly read -1 or 1. Usually joysticks are sensitive enough to sense that they've actually been moved to somewhere in between the center and the full axis, so that's what GetAxis tries to simulate. I'm unsure whether or not the speed of scaling can be adjusted by a variable, however it would make sense for Gravity to determine it, if anything in the Input settings.

Also, if I were you, I'd just use Unity's input manager, it's pretty good as is and is at least guaranteed to have equal or less bugs than a custom one. Besides, it works pretty well and has plenty of functionality. Some which you, like GetAxisRaw, didn't even notice.

As for how to determine the scaling speed of GetAxis, you'd likely just lerp the value, as said previously. Define some speed, and Vector2.Lerp the value.

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 StevenUnu · Sep 09, 2017 at 03:42 PM 1
Share

This answer says much but doesn't really answer my question. I was asking how are the values calculated. I didn't say that I need it to always return -1, 0 and 1.

avatar image
0

Answer by UnityDeveloper2 · Feb 28, 2018 at 04:19 AM

The behavior you're seeing is normal. GetAxis is a virtual joystick on top of GetAxisRaw, which executes a smoothing algorithm using a hidden velocity vector and public acceleration constants to reconcile its current output with its target output (GetAxisRaw).

Unity exposes constants which dictate the smoothing algorithm through the InputManager, which can be accessed via Edit->Project Settings->Input. Of particular importance are Gravity and Sensitivity, which impact how fast the joystick responds to non-input (0) and input (-1 and 1), respectively. A joystick mirroring the values of GetAxisRaw would have both a large Gravity and Sensitivity.

Comment
Add comment · 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
0

Answer by AndrewWhitton · Apr 23, 2021 at 02:07 PM

interesting, I need the gamepad to lerp from 0 to 1 etc but only the keyboard does. even if I use getaxis

seems coding a lerp for the button reaction is the way to go for me then

Comment
Add comment · 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

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

71 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Smooth value in Input System 0 Answers

how to use a float in a GUI.TextField? 6 Answers

Javascript int to float 1 Answer

changing floats by input C# 1 Answer

float = int / int float value always 0 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