I have
Vector2 movement = inputActions.Movement.Move.ReadValue();
Vector3 move = new Vector3(movement.x, 0.0f, movement.y);
if (move != Vector3.zero)
{
rb.AddForce(move * speed)
}
which works. It makes it so that my Player will move once I press WASD or the arrow keys
if i say
if (inputActions.Movement.Move.triggered)
{
rb.AddForce(move*speed)
}
it wont work. We’ll kinda but it moves slowly.
My question is why?
If I’m holding the W key down is the W key not constantly being triggered and thus returning true every frame for that function?
I’m a newb so its probably obvious
Thanks in advance!
ReadValue() is fetching the raw data from the bound control and will always return something, whereas triggered is dependent on the action and type of interaction(s) defined - ie. if you set up a Hold interaction, triggered will only be true once on the frame where the Hold time has elapsed (or, similarly, a Button will be triggered when the push threshold is crossed)
The extension functions that @samlletas posted in this thread maybe demonstrate the difference clearly.
In your example, if you only want to call AddForce() (or other things) if the inputs are returning something, you could either
-
do as you’re doing, check that the input is not zero - and that you have appropriate dead-zones set up for any analog inputs
-
check if the phase of the control is started or performed, meaning it has been activated by something
inputActions.Movement.Move.phase == InputActionPhase.Performed )```
- check if the activeControl is set, meaning something is bound and driving the action
```if ( inputActions.Movement.Move.activeControl != null )```
disclaimer - I've only just started working with the NIS so there may be more correct answers @Rene-Damm or others can provide...
1 Like