• 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
Question by bpears · Jan 08, 2013 at 07:12 AM · javascriptxbox controller

How to treat Input.GetAxis as Input.GetButtonDown?

The triggers on xbox controller are axis, and when switc$$anonymous$$ng between mouse triggers and xbox, they do not behave the sameway, because the xbox triggers are basically Input.GetButton, w$$anonymous$$ch causes the firing of weapons to shoot repeatedly.

I tried Input.GetAxisRaw and not$$anonymous$$ng changed.

Comment
SepM
Lohoris2
Cantankerous14
rad1c
$$anonymous$$
robertdmoores
ahungrybear
harmandragon9990
PonchoLira
Plasmajam
taxvi
MI_Buddy
YellowGD
TomaTantrum
KelvinRodri
And 2 more...

People who like this

17 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 rFinster · Aug 14, 2015 at 04:15 PM 3
Share

You can just use GetButtonDown with the name of an axis.

9 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by CodeMasterMike · Jan 08, 2013 at 10:16 AM

With the GetAxisRaw function you get either 0 or 1/-1 (depending on axis). So it does work like the GetButton function. If you want it to work as the GetButtonDown function, you need to add little more code.

 private bool m_isAxisInUse = false;
 
 void Update()
 {
     if( Input.GetAxisRaw("Fire1") != 0)
     {
         if(m_isAxisInUse == false)
         {
             // Call your event function here.
             m_isAxisInUse = true;
         }
     }
     if( Input.GetAxisRaw("Fire1") == 0)
     {
         m_isAxisInUse = false;
     }    
 }

In t$$anonymous$$s very simple example, you see that the event function will be called once, eventhough the axis-button keeps being pressed down. And when the axis-button is no longer pressed, the check gets restored, so that next input it will fire your event function again. I hope t$$anonymous$$s example helps you.

Good luck!

Comment
bpears
SeveneduS
DMCH
_1
tvwxyz
Smireles
SepM
ASPePeX
Nagapeso
Bitboys
Demonicdaron
Cantankerous14
Food_Lover
Haiku-Oezu
TheWarper
And 37 more...

People who like this

52 Show 8 · 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 bpears · Jan 10, 2013 at 03:59 AM 0
Share

how would this look in unityscript? and what do you put for axis in use? do you leave it as is, or do you really put the axis in use there? like m_9thAxis?

avatar image CodeMasterMike · Jan 10, 2013 at 06:06 AM 0
Share

I don't know unityscript, but if you follow the thread of thought, Im sure you can rewrite it to unityscript, since its not a complicated code:

  1. If your GetAxisRaw("Fire1") is pressed AND your "button is in use" is set to false, then set your "button is in use" boolean to true and do whatever you want to do when the button is pressed.

  2. if your GetAxisRaw("Fire1") is NOT pressed, then set your "button is in use" boolean to false.

You can call the boolean variable whatever you want. If you have several buttons that you want handle like above, then you would need to rename them to something properly, yes.

Easiest to make and read would be something like:

m_Fire1IsInUse m_Fire2IsInUse m_SpaceIsInUse etc. etc.

There are always ways making the code more dynamic without having to hard code all variables, and so on. But that is more advanced topic.

avatar image Smireles · May 21, 2014 at 04:08 AM 0
Share

Simple and useful answers always makes me happy. Thanks for this useful advice CodeMasterMike.

avatar image ThatRobRobinson · Feb 16, 2015 at 03:50 PM 0
Share

I am getting a "cannot implicitly convert type float to bool" error when implementing this. "fire 1" in my case is a bool. Do I just HAVE to recode some stuff and configure it to be a float since I am using an axis? I was using the Right Bumper on an xbox pad, but want the right trigger to do the job instead. GetButtonDown worked perfectly with the bumper. Advice real quick? :D

avatar image ThatRobRobinson · Feb 16, 2015 at 04:19 PM 0
Share

Re coding everything to be a float vs a bool worked perfectly. thanks!

Show more comments
avatar image

Answer by AndrewRyan · Feb 09, 2017 at 06:12 PM

I wasn't satisfied with t$$anonymous$$s answer, so I devised t$$anonymous$$s solution w$$anonymous$$ch I t$$anonymous$$nk is cleaner and modular and uses event delegates:

     //left trigger
     bool _lt;
     public delegate void LeftTrigger();
     public event LeftTrigger onLeftTrigger;
 
     void LTrigger()
     {
         if(!_lt && Input.GetAxis("360_Triggers") < 0f)
             if(onLeftTrigger != null) onLeftTrigger();
               _lt = true; 
         w$$anonymous$$le(Input.GetAxis("360_Triggers") < 0f && _lt) return;
              _lt = false;
     }
 
     void Update()
     {
         LTrigger();
     }
Comment
Plasmajam
cjhoffmn

People who like this

2 Show 0 · 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

Answer by Julianobsg · May 18, 2018 at 11:29 PM

I saw both answers recently, but wasn't happy with any of the given answers, in the documentation I found the value anyKeyDown So I managed to get a cleaner solution for me:

  void Update()
  {
     if(Input.anyKeyDown)
     {
         float horizontal = - Input.GetAxis("Horizontal");
     }
  }

Comment
_jonathan_

People who like this

1 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 JamesGMarks · Jun 03, 2018 at 03:58 PM 0
Share

Tried this method, since I agree that it looks cleaner. However, it only works for the keyboard and does not extend to controller input.

avatar image

Answer by KelvinRodri · Sep 02, 2018 at 11:58 PM

I was not so happy with those solutions, and I t$$anonymous$$nk that could exist one that is clear to read and easy to reuse. So I did the following code:

 using UnityEngine;
 
 public abstract class BaseJoystickInputs : ScriptableObject {
 
     bool _lastInputAxisState;
 
     /// <summary>
     /// Gets the axis input like an on key down event, returning <c>true</c> only 
     /// on the first press, after t$$anonymous$$s return <c>false</c> until the next press. 
     /// Only works for axis between 0 (zero) to 1 (one).
     /// </summary>
     /// <param name="axisName">Axis name configured on input manager.</param>
     protected bool GetAxisInputLikeOnKeyDown(string axisName)
     {
         var currentInputValue = Input.GetAxis(axisName) > 0.1;
 
         // prevent keep returning true when axis still pressed.
         if (currentInputValue && _lastInputAxisState)
         {
             return false;
         }
 
         _lastInputAxisState = currentInputValue;
 
         return currentInputValue;
     }
 }
 

Please let me know any doubts/suggestions.

Comment
spiraloid

People who like this

1 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 KelvinRodri · Sep 03, 2018 at 12:07 AM 0
Share

@JamesGMarks hello, idk if you have found your solution, but maybe this can help.

avatar image

Answer by toomasio · Nov 25, 2018 at 07:55 PM

You can create a custom class for t$$anonymous$$s:

 [System.Serializable]
 public class InputProperty
 {
     public string stringValue;
     public int indexValue;
 
     private bool InputDown { get { return Input.GetButtonDown(stringValue) || Input.GetAxisRaw(stringValue) != 0; } }
     private bool lastDownCheck = false;
     private bool lastUpCheck = true;
 
     public bool GetInput()
     {
         return InputDown;
     }
 
     public bool GetInputDown()
     {
         if (InputDown)
         {
             if (InputDown != lastDownCheck)
             {
                 lastDownCheck = InputDown;
                 return true;
             }
 
         }
         lastDownCheck = InputDown;
         return false;
     }
 
     public bool GetInputUp()
     {
         if (!InputDown)
         {
             if (!InputDown != lastUpCheck)
             {
                 lastUpCheck = !InputDown;
                 return true;
             }
 
         }
         lastUpCheck = !InputDown;
         return false;
     }
 }
Comment

People who like this

0 Show 0 · 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
  • 1
  • 2
  • ›

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

27 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

Related Questions

Multiple Cars not working 1 Answer

Shared variable problem, between scripts, help? 1 Answer

How to ignore specific collider, non-raycast? 3 Answers

Does the -OnTriggerStay collider- script have to be child of collider triggering the function? 2 Answers

Can someone explain the numbers associated with xbox controller triggers? 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