Movement value is never captured from ReadValue

For some reason, I can never get movement to capture/recognize any key input. Are there any common reasons I can investigate?

I’ve confirmed the ‘Player’ setting is set to the ‘both’ option for input handling. My code does not generate any debugger errors. I’ve tried setting the input action to WASD and arrow keys. PlayerController and RigidBody is attached to the player sprite. I can accomplish movement by passing in a dummy Vector2 object, but I cannot get movement to report any new values from the keyboard input.

Here is my code:

public class PlayerController : MonoBehaviour

{

[SerializeField] private float moveSpeed = 1f;

private PlayerControls playerControls;

private Vector2 movement;

private Rigidbody2D rb;

private void Awake() {

    playerControls = new PlayerControls();

    rb = GetComponent<Rigidbody2D>();

}

private void onEnable() {

    playerControls.Enable();

}

private void OnDisable() {

    playerControls.Disable();

}

private void Update() {

    PlayerInput();

}

private void FixedUpdate() {

    Move();

}

private void PlayerInput() {

    movement = playerControls.Movement.Move.ReadValue<Vector2>();

   

    Debug.Log(movement.x);

}

private void Move() {

    rb.MovePosition(rb.position + movement * (moveSpeed * Time.fixedDeltaTime));

}

}

Here is the Player Controls

You never enable the controls. You have onEnable() to do it, but that’s never being called because it should be OnEnable()

2 Likes

Thank you so much, I figured it was something simple like that.
I know one way to know the correct method is to peruse the unity API documentation. I think I can take it to the next level by following instructions like these for Intellisense to be smart enough to show available functions while typing. c# - How to get intellisense in Visual Studio Code for Unity functions names? - Stack Overflow

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms