- Home /
New Input system holding down a button
I want to have my players movement speed change if the run button is held down. The action is held in a private InputAction runAction;
I am unsure on how to have an if statement in update check if the button is being held down and then call a function to change the movement speed.
Answer by tyruji · Jul 22, 2021 at 08:41 PM
You could subscribe methods to this action, which would be setting a value of a bool field in your class. Would look something like this:
// ...
private InputAction _runAction;
private bool _isRunning = false;
private void Awake()
{
_runAction.performed += ( c ) => { _isRunning = true; };
_runAction.canceled += ( c ) => { _isRunning = false; };
}
// do stuff ...
The "performed" event is called when you press a button, while "canceled" is called when you release it. Well not in all cases, but most.
Your answer
Follow this Question
Related Questions
Input.GetMouseButtonDown(0) running through my if statments too quickly 1 Answer
lerp to 0, problem with horizontal value 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Variable continuously add up in Update() 1 Answer
run code once every 10 second 1 Answer