- Home /
Postive and Negative Button Binding
I was wondering, it is possible to bind positive and negative buttons as a command. Like with GetButtonDown. I have a script where if you hit left or right certain animations play, but how do I rebind it so that players can re-assign these keys?
I know there is a way to try this, I ran the game myself seeing if changing those keys would work. Nope, didn't.
Answer by Democide · Jul 07, 2014 at 09:22 AM
What you can do is get the button and then check for the corresponding axis, like so:
if (Input.GetButtonDown("Horizontal") && Input.GetAxisRaw("Horizontal") > 0) {
// Move to the right
}
else if (Input.GetButtonDown("Horizontal") && Input.GetAxisRaw("Horizontal") < 0) {
// Move to the left
}
Also be aware that the axis is set to SNAP. This means a change of direction instantly sets the axis to 0.
I have a character and I want to jump with ("vertical") Positive if with that code will work?
this code worck bat i have 1 problem
m_Jump = CrossPlatformInput$$anonymous$$anager.GetButtonDown("p1 Jamp");
if i change this to m_Jump = CrossPlatformInput$$anonymous$$anager.GetButton("p1 Vertical") && Input.GetAxisRaw("p1 Vertical") > 0; the jamp is no the same this coede is the Platformer2DUserControl
Answer by fschneider · Jul 07, 2014 at 09:25 AM
The documentation linked above is saying you should use
value = Input.GetAxis ("Horizontal");
and then check whether the value is above or below zero.
Well that works, but it fires EVERY FRA$$anonymous$$$$anonymous$$ GetButtonDown only fires in the frame that the button was pressed. So these are used for different cases.
Answer by MasterBLB · Jul 18, 2011 at 07:15 AM
In the doc for GetKeyDown there is mentioned to use GetButton instead,as it allows binding,while GetKeyDown works on enum: http://unity3d.com/support/documentation/ScriptReference/Input.GetKeyDown.html
So how would I make the command for GetButtonDown?
I just tried GetButtonDown("right") and that sure didn't work.
Well,try to run your project on PC,and when resolution∈put dialog shows switch tab to input and see what bind is for "right",then press that key and see if it works.
I see there is no something like "right",but the control name is "Horizontal(+)"
I did both GetButtonDown("Horizontal(+)") and GetAxis("Horizontal(+)") and neither worked.
Hmm..and did you add Input$$anonymous$$anager component to a mesh containing your script?The doc is not clear about it,but I bet it's necessary in order to work
http://unity3d.com/support/documentation/$$anonymous$$anual/Input.html
See my solution below: The issue is: GetButtonDown("Horizontal") fires for both the positive and the negative button. You need to do this but also check for the value of the axis to figure out if you're pressing the positive or negative button for that axis. Then you can use that to control your game. I just did it and it worked.