Set binding parameters in code
Get and override the parameters of the processors, interactions, and composites on your bindings from your scripts.
The examples on this page use the following variables:
action: anInputAction.mapandasset: theInputActionMapandInputActionAssetthat contain the action.
Set default parameter values
Processors, interactions, and composites can have parameters. You can set the default values of these parameters in the Binding Properties panel of the Input Actions Editor window. For more information, refer to Binding Properties panel reference.
You can also set the default values in the strings you pass to the API:
// Create an action with a Hold interaction, and set its duration parameter to 4 seconds.
var holdAction = new InputAction(interactions: "hold(duration=4)");
// The Input System creates the interaction when it resolves the action's bindings,
// so add at least one binding before you read the parameter.
holdAction.AddBinding("<Gamepad>/buttonSouth", groups: "Gamepad");
holdAction.AddBinding("<Keyboard>/space", groups: "Keyboard&Mouse");
Get parameter values
To get the current value of a parameter, use GetParameterValue. It returns a PrimitiveValue?, which is null if the Input System doesn't find the parameter. The Input System creates processors, interactions, and composites when it resolves an action's bindings, so GetParameterValue returns null until the action has at least one binding. Otherwise, you can convert the value to a number or a Boolean. To get a parameter value:
var duration = holdAction.GetParameterValue("duration");
Debug.Log("'duration' is set to: " + duration.Value);
By default, GetParameterValue looks for the parameter on every object on every binding of the action. To narrow the search to one type of object, or to certain bindings, pass a more specific name or a binding mask:
// Get the duration parameter of the Hold interaction, only on bindings in the "Gamepad" group.
holdAction.GetParameterValue("hold:duration", InputBinding.MaskByGroup("Gamepad"));
You can also pass an expression that names both the type and the parameter. This version checks the names when your script compiles, and returns the parameter's own type, such as float?, instead of a PrimitiveValue?.
// Get the duration parameter of HoldInteraction as a float?.
holdAction.GetParameterValue((HoldInteraction x) => x.duration);
Override parameter values
To change the value of a parameter, apply a parameter override with ApplyParameterOverride. You can apply an override to an action, to an action map, or to an action asset. The Input System applies the new value immediately to all the composites, processors, and interactions the override targets, and also to bindings you add later. To override parameter values:
// Set the duration parameter on all bindings of the action to 4.
action.ApplyParameterOverride("duration", 4f);
// Set the duration parameter of Tap interactions only.
action.ApplyParameterOverride("tap:duration", 0.5f);
// Set the duration parameter of Tap interactions, only on bindings in the "Gamepad" group.
action.ApplyParameterOverride("tap:duration", 0.5f, InputBinding.MaskByGroup("Gamepad"));
// Set the duration of Tap interactions on all bindings in an action map, or in an entire asset.
map.ApplyParameterOverride("tap:duration", 0.5f);
asset.ApplyParameterOverride("tap:duration", 0.5f);
// As with GetParameterValue, you can pass an expression instead of a name.
action.ApplyParameterOverride((TapInteraction x) => x.duration, 0.4f);
map.ApplyParameterOverride((TapInteraction x) => x.duration, 0.4f);
asset.ApplyParameterOverride((TapInteraction x) => x.duration, 0.4f);
If several overrides target the same parameter on a binding, the Input System compares them in the following order and applies the first one that wins:
- An override that names the object type, such as
tap:duration, wins over one that doesn't, such asduration. - An override that targets a binding path wins over one that doesn't.
- An override on an action wins over one on an action map or asset.
- Otherwise, the override you applied first wins. The Input System checks overrides on the action map before overrides on the asset.
Restricting an override to a binding group, for example with InputBinding.MaskByGroup, doesn't make it more specific. To give some of an action's bindings a different value, apply the more restricted override first:
// Apply a Tap duration override to the action, only for bindings in the "Gamepad" group.
action.ApplyParameterOverride("tap:duration", 1f, InputBinding.MaskByGroup("Gamepad"));
// Apply another override to all bindings of the same action.
action.ApplyParameterOverride("tap:duration", 0.6f);
// Apply a third override to the entire asset.
asset.ApplyParameterOverride("tap:duration", 0.3f);
// Now bindings on the action in the "Gamepad" group use a Tap duration of 1,
// other bindings on the action use 0.6, and all other bindings in the asset use 0.3.
For example, you can use parameter overrides to set the mouse and gamepad sensitivity of a look action separately:
// Set up a "look" action.
var look = new InputAction("look", type: InputActionType.Value);
look.AddBinding("<Mouse>/delta", groups: "Keyboard&Mouse", processors: "scaleVector2");
look.AddBinding("<Gamepad>/rightStick", groups: "Gamepad", processors: "scaleVector2");
// Scale mouse and stick input separately.
look.ApplyParameterOverride("scaleVector2:x", 0.5f, InputBinding.MaskByGroup("Keyboard&Mouse"));
look.ApplyParameterOverride("scaleVector2:y", 0.5f, InputBinding.MaskByGroup("Keyboard&Mouse"));
look.ApplyParameterOverride("scaleVector2:x", 2f, InputBinding.MaskByGroup("Gamepad"));
look.ApplyParameterOverride("scaleVector2:y", 2f, InputBinding.MaskByGroup("Gamepad"));
// Instead of a group, you can also target bindings by their path.
look.ApplyParameterOverride("scaleVector2:x", 0.5f, new InputBinding("<Mouse>/delta"));
look.ApplyParameterOverride("scaleVector2:y", 0.5f, new InputBinding("<Mouse>/delta"));
Note
The Input System doesn't save parameter overrides with an asset.