- Home /
Joystick 10th axis code problem
i have an interesting problem, this piece of code is inside the Update function and everything works fine besides "Input.GetAxis("Fire1Jostick") > 0.5" when i pull the trigger the second axis is repeatedly fired causing the player to shoot 6 pistol rounds instantly
if(Input.GetButtonDown("Fire1") || Input.GetAxis("Fire1Jostick") > 0.5){
if(Trigger == true){
Shoot();
Trigger = false;
}else{
Trigger = true;
}
}
and this problem has not happened with the button so i know the code works(for a button)
BTW the Fire1Jostick is Axis 10(for xbox360 controller)
Thanks
Answer by s_guy · Aug 12, 2013 at 09:17 PM
The Update function is called every frame and Input.GetButtonDown() is only true for the one frame that the button is pressed. (See http://docs.unity3d.com/Documentation/ScriptReference/Input.GetButtonDown.html) So, your condition to fire is true only once when using that button and you get one call to Shoot().
On the other hand, Input.GetAxis() reports the status of the axis every frame. Since you won't likely have the trigger pulled for only one frame, your condition to fire is true for several frames. The same sort of thing will happen when you use Input.GetButton(), which reports true not just for the frame it's pressed, but for every frame while it's held. So, you get many calls to Shoot().
They typical solution for a single shot with this kind of input, is to save a bool for if a shot was fired and only reset this once the button up condition is met.
Here is an answer with a code example http://answers.unity3d.com/questions/376587/how-to-treat-inputgetaxis-as-inputgetbuttondown.html
If you wanted auto-fire, you could watch Time.time to see if enough time has passed since the last bullet was shot. Then that will be an additional condition for shooting that bullet.
Your answer
![](https://koobas.hobune.stream/wayback/20220613101545im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Main Menu: Is it better to subdivide it into different scenes? 1 Answer
How can I play with one joystick? 0 Answers
Combining first person contoller and dual touchpads from standard assets 0 Answers