x


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 '13 at 07:12 AM

bpears gravatar image

bpears
490 63 135 168

(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 '13 at 10:16 AM

CodeMasterMike gravatar image

CodeMasterMike
2k 1 3 10

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 '13 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 '13 at 06:06 AM CodeMasterMike
(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

By RSS:

Answers

Answers and Comments

Topics:

x3388
x398
x19

asked: Jan 08 '13 at 07:12 AM

Seen: 3164 times

Last Updated: Jan 10 '13 at 06:13 AM