'How to override InputSystem Action path?
I have a rebind script in my game that looks like this:
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.EventSystems;
using TMPro;
public class RebindButton : MonoBehaviour
{
[SerializeField] private InputActionReference inputActionRef;
[SerializeField] private TMP_Text buttonText;
[SerializeField] private int actionIndex = 0;
private InputActionRebindingExtensions.RebindingOperation rebindingOperation;
private void Start()
{
buttonText.text = InputControlPath.ToHumanReadableString(inputActionRef.action.bindings[actionIndex].effectivePath,
InputControlPath.HumanReadableStringOptions.OmitDevice);
}
public void StartRebinding()
{
if (inputActionRef.action.bindings[actionIndex].isPartOfComposite)
{
Debug.Log(inputActionRef.action.bindings[actionIndex].path);
}
rebindingOperation = inputActionRef.action.PerformInteractiveRebinding(actionIndex)
.WithCancelingThrough("<Keyboard>/escape")
.OnMatchWaitForAnother(0.1f)
.OnCancel(operation => RebindCancel())
.OnComplete(operation => RebindComplete())
.Start();
}
private void RebindComplete()
{
Debug.Log(inputActionRef.action.bindings[actionIndex].path);
buttonText.text = InputControlPath.ToHumanReadableString(inputActionRef.action.bindings[actionIndex].effectivePath,
InputControlPath.HumanReadableStringOptions.OmitDevice);
rebindingOperation.Dispose();
FindObjectOfType<EventSystem>().SetSelectedGameObject(null);
}
private void RebindCancel()
{
rebindingOperation.Dispose();
FindObjectOfType<EventSystem>().SetSelectedGameObject(null);
}
}
But all bindings that I change keep the same action path as before, even though their inputaction.callback button changing according to rebinding. That makes impossible to change my composite Vector2 movement binding. Vector2 changing only when I press button that showed in the action path. My movements set to WASD. When I changing it to the arrows my hero starting to move only when I press one of the arrows and one of WASD buttons. His direction changes according to the WASD buttons I press. Here is some movement script code:
void Update()
{
if (moveButtonIsHeld)
{
direction = playerInput.PlayerMovement.Movement.ReadValue<Vector2>();
areTwoKeysPressed = direction.x != 0 && direction.y != 0;
Move();
}
else
playerComponent.animator.SetLayerWeight(1, 0);
}
public void SetStateMoveButton(InputAction.CallbackContext context)
{
if (context.performed)
moveButtonIsHeld = true;
if (context.canceled)
moveButtonIsHeld = false;
}
public void Move()
{
transform.Translate(direction * speed * Time.deltaTime);
if (direction.x != 0 || direction.y != 0)
SetAnimatorMovement(direction);
}
Also I would like to ask how to get actionIndex in the script. Now I'm setting it manually in the inspector.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
