Skip to main content
Post Reopened by DMGregory
Describe issue in title, share errors as text
Source Link
DMGregory
  • 141k
  • 23
  • 258
  • 401

I tried to make movement with animation on unity and had this Cannot read value of type 'Vector2' from WASD KeyControl

enter image description here

using System.Collections; using System.Collections.Generic; using I'm getting these error messages when I try to use Unity's new Input System.Runtime.CompilerServices; using TMPro; using UnityEngine; using UnityEngine.InputSystem;

public class PlayerMovement package: 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();
}

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();
    }
}

I tried to make movement with animation on unity and had this

enter image description here

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();
}

}

Cannot read value of type 'Vector2' from WASD KeyControl

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();
    }
}
added 78 characters in body
Source Link

enter image description here

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

public class PlayerMovement : MonoBehaviour {

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();
    }
}

}

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();
    }
}

enter image description here

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();
}

}

Post Closed as "Needs details or clarity" by liggiorgio, DMGregory
Formatting
Source Link
Kevin
  • 7k
  • 1
  • 12
  • 32

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

public class PlayerMovement : MonoBehaviour {

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();
    }
}

}

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();
}

}

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();
    }
}
Source Link
Loading