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
1
Question by chainedlupine · May 05, 2012 at 05:56 AM · inputaxisgetbutton

Input: Mixing axis and buttons (ie: Input.GetAxis versus Input.GetButton)

In my game, I use the vertical and horizontal axis (as defined by default in InputManager) in order to control my character.

However, I'd like to be able to use the GetButton functions for the positive/negative keys for the Vertical axis along with using GetAxis to get an analog value. Is there a way to do this?

So in other words, in some parts of my code I do this:

 throttle = Input.GetAxis("Vertical") ;

But then I would like to do this, too:

 if (Input.GetButtonDown (???))   
 // where ??? would be Vertical's positive button (plus the alt button)
Comment
Add comment · Show 4
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 hijinxbassist · May 05, 2012 at 06:35 AM 0
Share

@chainedlupine This might help for the http://unity3d.com/support/documentation/ScriptReference/Event-alt>alt button. Im not sure what ur saying here "I'd like to be able to use the GetButton functions for the positive/negative keys for the Vertical axis along with using GetAxis" The negative and positive values of GetAxis are -1 and 1.

Can you explain what you are trying to do in more detail.

avatar image syclamoth · May 05, 2012 at 06:47 AM 0
Share

Just define a new button in the input manager, that takes the same input. Use the axis for the analog stuff, and the button for the toggle stuff.

avatar image chainedlupine · May 05, 2012 at 06:52 AM 0
Share

alt button, as it "alternative button." By default, Vertical is mapped to the up/down arrow keys, plus the W/S keys, plus joystick axis #0.

I know I can get the analog values of that axis by using GetAxis. But I would also like to access the actual button states for the buttons that make up that axis.

So: GetAxis("Vertical") in order to read it as a -1..1 value, or use GetButton("Vertical Positive Button") or GetButton("Vertical Negative Button") in order to treat it like a binary button.

To clarify, currently in my code I operative a vehicle in two modes. One is an airplane mode, where the vertical axis access controls the throttle. But that vehicle also transforms into a walking mode, where vertical should no longer function as a throttle but as a jump button. But I don't want to have a separate Input$$anonymous$$anager define for Jump -- I don't need it, Unity already knows that Vertical axis corresponds to the Up Arrow (aka W key).

I also don't want to use Input.Get$$anonymous$$eyXXX($$anonymous$$eyCode.Up) because that would be a hard-coded dependency upon the Up Arrow. I'd rather use the Input$$anonymous$$anager.

avatar image chainedlupine · May 05, 2012 at 07:40 AM 0
Share

@syclamoth: Yeah I can do it that way but I don't want to, as it means two keybinds to config in the launcher. That could lead to an ugly situation where a user configures the Vertical keybinds but doesn't realize the Jump keybind applies, too.

Since Unity already knows what that key is, if I could just access it then I could use the Get$$anonymous$$ey methods directly.

4 Replies

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

Answer by chainedlupine · May 05, 2012 at 07:39 PM

I had the idea to just write a button-press simulator on my own, but then I ran into the problem of making sure that I get these changes to my scripts in a timely fashion. Doing a ButtonDown or Up event requires tracking the state of the input across a frame.

So, in short I would do this:

  1. Read state of axis, generate the appropriate button up/down states

  2. Do my usual Update logic that relies upon these states.

  3. Repeat per frame.

Unfortunately, Unity only provides us with a way to delay per-frame processing (via LateUpdate) but no way to do processing before a frame begins. (ie: No PreUpdate.) Therefore, step #1 is never guaranteed to be complete before step #2. (Which can run in any order, AFAIK, though I haven't tested this rigorously but it just makes sense.)

I could do step #1 in step #2 for each time I want to use these inputs, ie: such as...

void Update() { InputLibrary.assignAxisButtonStates ("Vertical") ;

if (InputLibrary.GetAxisAsButton ("Vertical", InputLibrary.UpButton)) { // equiv to Input.GetButton() // ie: Vertical is currently in positive analog range }

if (InputLibrary.GetAxisAsButtonDown ("Vertical", InputLibrary.UpButton)) { // equiv to Input.GetButtonDown() // ie: Vertical was shifted from dead zone to positive analog range since last frame }

if (InputLibrary.GetAxisAsButtonUp ("Vertical", InputLibrary.UpButton)) { // equiv to Input.GetButtonUp() // ie: Vertical was shifted from positive analog range to dead zone since last frame } }

But it's a little ugly. assignAxisButtonStates would need to keep track to make sure it isn't called multiple times per frame.

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
1

Answer by cookiepuss · Sep 03, 2012 at 10:31 AM

 This works for what you need. This is for a cursor I made.

 void Update()
 {   
         if (toggleOn == true)
             {
         if (Input.GetAxis ("Vertical") > 0)
                 {
                     cursormove += 20;
                     toggleOn = false;
                 }
         if (Input.GetAxis ("Vertical") < 0)
                 {
                     cursormove -= 20;
                     toggleOn = false;
                 }
             }
     
             // do stuff
     
         if (Input.GetAxis ("Vertical") == 0)
                 {
                     toggleOn = true;
                 }
 }
 
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 Pandiux · May 05, 2012 at 08:03 AM

Input.GetAxis gives a value more than 0 if being pressed and a negative value if not so you could use

 if (Input.GetAxis("Vertical") < 0){
 //GO BACK GODE
 }
 if (Input.GetAxis("Vertical") > 0){
 //GO FORWARD GODE
 }

or you could do what many people do which is just move to the direction of the axis, for example

 transform.Translate(0,0, 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 chainedlupine · May 05, 2012 at 08:08 AM 0
Share

I know that.

I want to use the GetButtonXXX methods on Input in order to read the binary state of the Vertical axis buttons.

I want to use both Vertical as an analog axis and as an on/off button. See my reply to hijinxbassist above for more detail.

avatar image
0

Answer by naglers · Jul 22, 2015 at 07:20 PM

based off of this answer http://answers.unity3d.com/questions/376587/how-to-treat-inputgetaxis-as-inputgetbuttondown.html

You can use Input.GetAxisRaw

to get button states like so:

 if(Mathf.Approximately(Input.GetAxisRaw("Horizontal"), -1))
 {
     //Left
 }
 else if(Mathf.Approximately(Input.GetAxisRaw("Horizontal"), 1))
 {
     //Right
 }

this works because no smoothing is applied to GetAxisRaw, so if you are holding your key you get that value (I used Mathf.Approx because they are floats and might not be exactly -1/1 just in case)

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

9 People are following this question.

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

Related Questions

Press button once to enter vehicle and once again to exit 1 Answer

Microphone input volume to axis value translation 1 Answer

Why does holding down any button cause ~20-30 FPS loss? 1 Answer

Reading 'not defined' Controller Axis Input? 0 Answers

Problem with input code for a camera follow/rotate script 0 Answers


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