Small glitch in my event-based input manager

Recently, I decided to abstract input away from the character and into a single InputManager class. This way, player input can be switched to different characters on the fly. I’m already familiar with regular C# events, but I thought I’d give the UnityEvent class a try.

99% of the time, this works as intended and is very responsive. However, every once in a while, the character continues to move for a second or two after the movement key is released.

I figure that it’s probably some sort of timing issue. If somebody could offer any insight, I’d really appreciate it. Code is below.

public class InputManager : MonoBehaviour
  {
  private Vector3 inputMoveDirection;

  public InputMoveEvent inputMove;
   
  void Start()
  {
  inputMoveDirection = new Vector3(0f, 0f, 0f);

  if (inputMove == null)
  {
  inputMove = new InputMoveEvent();
  }

  // TODO: Temporary Test Code!
  CharacterControl controller = GameObject.FindGameObjectWithTag("Player").GetComponent<CharacterControl>();
  inputMove.AddListener(controller.OnInputMove);
  }

  void Update()
  {
  inputMoveDirection.x = Input.GetAxis("Horizontal");
  inputMoveDirection.z = Input.GetAxis("Vertical");

  inputMove.Invoke(inputMoveDirection);
  }
  }

  public class InputMoveEvent : UnityEvent<Vector3> { }
public class CharacterControl : MonoBehaviour
    {
        private Rigidbody myRigidbody;
        private CharacterState characterState;

        void Start()
        {
            myRigidbody = GetComponent<Rigidbody>();
            characterState = this.GetComponent<CharacterState>();
        }
       
        public void OnInputMove(Vector3 inputMoveDirection)
        {
            // TODO: This works 99% of the time, but every once in a while, the character continues to move for a second after input has been released
            if (characterState.isGrounded)
            {
                myRigidbody.velocity = inputMoveDirection.normalized * characterState.moveSpeed;
            }
        }
    }

Not sure but GetAxis gives “smoothed value”,
this is without smoothing: Unity - Scripting API: Input.GetAxisRaw

No luck on that fixing the problem, but still a good thing to know.

Thanks for the tip. As I move forward, I’ll have to experiment to see which one feels better for my game.

I think I’ve got it. It came down to several factors which combined to create the problem:

  • I was using “normalize” to ensure prevent the character from going faster than moveSpeed when moving diagonally.
  • I’m setting the rigidbody’s velocity rather than using Translate or AddForce, because I want precise movement that still respects physics.
  • Using GetAxis instead of GetAxisRaw, like mgear said.

I was testing with both the keyboard and a controller, and there were a few cases where the stick stopped slightly outsize the deadzone, but normalizing the vector caused the character to still move at full speed.

For now, I think I’m going to use Vector3.ClampMagnitude to slow down diagonal movement, but I might change it later when I add a “run” button.