Before updating to Windows 10 you may need to return your Pro license. More information here

How to treat Input.GetAxis as Input.GetButtonDown?

The triggers on xbox controller are axis, and when switching between mouse triggers and xbox, they do not behave the sameway, because the xbox triggers are basically Input.GetButton, which causes the firing of weapons to shoot repeatedly.

I tried Input.GetAxisRaw and nothing changed.

more ▼

asked Jan 08, 2013 at 07:12 AM

avatar image

586 293 1425 368

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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 this 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 this example helps you.

Good luck!

more ▼

answered Jan 08, 2013 at 10:16 AM

avatar image

2.3k 6 11 22

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?

Jan 10, 2013 at 03:59 AM bpears

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.

Jan 10, 2013 at 06:06 AM CodeMasterMike

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

May 21, 2014 at 04:08 AM mireles001

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

Feb 16 at 03:50 PM ThatRobRobinson

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

Feb 16 at 04:19 PM ThatRobRobinson
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Jan 08, 2013 at 07:12 AM

Seen: 9880 times

Last Updated: Feb 16 at 04:19 PM