2
\$\begingroup\$

I'm getting these error messages when I try to use Unity's new Input System package:

Cannot read value of type 'Vector2' from control '/Keyboard/w' bound to action 'CharacterControls/Movement[/Keyboard/w,/Keyboard/a,Keyboard/s,/Keyboard/d]' (control is a 'KeyControl'

InvalidOperationException while executing 'performed' callbacks of 'CharacterControls/Movement[/Keyboard/w,/Keyboard/a,Keyboard/s,/Keyboard/d]'

Here is the code I'm using:

using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;


public class PlayerMovement : MonoBehaviour
{

    private CharacterController _charactercontroller;
    [SerializeField] private Vector3 _walkDirection;
    [SerializeField] private Vector3 _velocity;
    [SerializeField] private float _speed;
    Animator animator;

    int isWalkingHash;
    int isRunningHash;

    PlayerInput input;

    Vector2 currentMovement;
    bool movementPressed;
    bool runPressed;

    void Awake()
    {
        input = new PlayerInput();

        input.CharacterControls.Movement.performed += ctx =>
        {
            currentMovement = ctx.ReadValue<Vector2>();
            movementPressed = currentMovement.x != 0 || currentMovement.y != 0;
        };
        
        input.CharacterControls.Run.performed += ctx => runPressed = ctx.ReadValueAsButton();
    }

    void Start()
    {
        animator = GetComponent<Animator>();
        _charactercontroller = GetComponent<CharacterController>();

        isWalkingHash = Animator.StringToHash("isWalking");
        isRunningHash = Animator.StringToHash("isRunning");
    }

    void Update()
    {
        handleMovement();
    }
 
    void handleMovement()
    {
        bool isRunning = animator.GetBool(isRunningHash);
        bool isWalking = animator.GetBool(isWalkingHash);

        if ( movementPressed && isWalking) {
            animator.SetBool(isWalkingHash, true);
        }
        if (!movementPressed && !isWalking)
        {
            animator.SetBool(isWalkingHash, false);
        }
        if((movementPressed && runPressed) && isRunning)
            {
                animator.SetBool(isRunningHash, true);
            }
        if((movementPressed || !runPressed) && isRunning)
            {
                animator.SetBool(isRunningHash, false);
            }
        
    }

    private void OnEnable()
    {
        input.CharacterControls.Enable();
    }

    private void OnDisable()
    {
        input.CharacterControls.Disable();
    }
}
\$\endgroup\$
0

1 Answer 1

5
\$\begingroup\$

The error message is telling you that you're trying to read a Vector2 value from a keyboard key that is currently treated as a Button input. A Button input doesn't have a Vector2 value; a Button is either pressed or not pressed.

If you want to read WASD input as a Vector2, you need to set the Action Type to "Value" (rather than "Button"), set the control type to "Vector 2", and add a 4-way composite binding:

Adding a composite binding

This will tell Unity to treat the 4 keys as a composite input with two axes. You can then read a Vector2 value from the composite in the same way as you would from an analog stick.

The exact appearance and labeling of this interface will depend on the version of the Input System package that you are using.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .