Add composite bindings in code
Add composite bindings to actions from your scripts, bind each part to one or more controls, and set the composite's parameters.
For an introduction, refer to Composite bindings.
The examples on this page use the following variables:
playerInput: aPlayerInputcomponent.action: anInputAction.
Add a composite binding
To add a composite binding, call AddCompositeBinding with the name of the composite type, then call With once for each part:
// Add a WASD composite binding to the "move" action.
playerInput.actions["move"]
.AddCompositeBinding("2DVector")
.With("Up", "<Keyboard>/w")
.With("Down", "<Keyboard>/s")
.With("Left", "<Keyboard>/a")
.With("Right", "<Keyboard>/d");
In the action's bindings, a composite binding is one binding that has isComposite set to true, followed by one binding for each part, which has isPartOfComposite set to true. So several consecutive entries in InputActionMap.bindings or InputAction.bindings form one composite.
You can bind each part as many times as you need:
// Make both triggers and both shoulder buttons pull on the axis.
action.AddCompositeBinding("1DAxis")
.With("Positive", "<Gamepad>/rightTrigger")
.With("Positive", "<Gamepad>/rightShoulder")
.With("Negative", "<Gamepad>/leftTrigger")
.With("Negative", "<Gamepad>/leftShoulder");
Like interactions and processors, composites can have parameters. To set them, add them in parentheses after the composite type:
// Make the positive side win when both sides are actuated.
action.AddCompositeBinding("1DAxis(whichSideWins=1)")
.With("Positive", "<Gamepad>/rightTrigger")
.With("Negative", "<Gamepad>/leftTrigger");
Use the name in code of the composite type, such as 1DAxis or 2DVector, and the part names in code, such as Positive or Up. Part names aren't case-sensitive. For the names, parts, and parameters of each composite type, refer to Composite types reference. To create your own composite type, refer to Create custom composite bindings.
Examples for each composite type
The following examples add one composite binding of each type.
1D axis
For the parts and parameters, refer to 1D axis.
action.AddCompositeBinding("1DAxis")
.With("Positive", "<Gamepad>/rightTrigger")
.With("Negative", "<Gamepad>/leftTrigger");
2D vector
For the parts and parameters, refer to 2D vector.
action.AddCompositeBinding("2DVector")
.With("Up", "<Keyboard>/w")
.With("Down", "<Keyboard>/s")
.With("Left", "<Keyboard>/a")
.With("Right", "<Keyboard>/d");
// Set the mode to analog (mode=2).
action.AddCompositeBinding("2DVector(mode=2)")
.With("Up", "<Gamepad>/leftStick/up")
.With("Down", "<Gamepad>/leftStick/down")
.With("Left", "<Gamepad>/leftStick/left")
.With("Right", "<Gamepad>/leftStick/right");
3D vector
For the parts and parameters, refer to 3D vector.
action.AddCompositeBinding("3DVector")
.With("Up", "<Keyboard>/e")
.With("Down", "<Keyboard>/q")
.With("Left", "<Keyboard>/a")
.With("Right", "<Keyboard>/d")
.With("Forward", "<Keyboard>/w")
.With("Backward", "<Keyboard>/s");
One modifier
For the parts and parameters, refer to One modifier.
// Add a binding for Ctrl+1.
action.AddCompositeBinding("OneModifier")
.With("Binding", "<Keyboard>/1")
.With("Modifier", "<Keyboard>/ctrl");
// Add a binding to mouse delta that only takes effect while the Alt key is down.
action.AddCompositeBinding("OneModifier")
.With("Binding", "<Mouse>/delta")
.With("Modifier", "<Keyboard>/alt");
Two modifiers
For the parts and parameters, refer to Two modifiers.
// Add a binding for Ctrl+Shift+1, with either Ctrl key and either Shift key.
action.AddCompositeBinding("TwoModifiers")
.With("Binding", "<Keyboard>/1")
.With("Modifier1", "<Keyboard>/leftCtrl")
.With("Modifier1", "<Keyboard>/rightCtrl")
.With("Modifier2", "<Keyboard>/leftShift")
.With("Modifier2", "<Keyboard>/rightShift");