How to use new Input System to move without "Expensive method invocation"?

Hi All,

How can I implement movement using new Input System with performance in mind?

Here is the simple movement method:

private void MovePlayer()
{
    Vector2 input = _playerInputActions.Player.Movement.ReadValue<Vector2>();

    _currentMovementVector = Vector2.SmoothDamp(_currentMovementVector, input, ref _smoothInputVelocity, SmoothMaxSpeed);

    transform.Translate(_currentMovementVector * (Speed * Time.deltaTime));
}

This has to be called in Update(), but reading the Vector2 value in each frame is quite expensive (get the performance warnings as well.

How do I implement this the better way?

Hi!

Are you sure this has any real impact on performance? I see nothing here that would cause any noticeable slowdown.

Run benchmarks! Check your framerate now without any changes, and then do another test with leaving the MovePlayer method empty. Let me know if there is a slowdown, however I expect there won’t be any.

Worrying about performance too early is common but usually unnecessary. Update loops and others that check every frame are not inherently bad for performance. Doing some already heavy calculation within those can be, however there is nothing of the sort in your MovePlayer method. Often you’ll find bigger performance gains to be made in things like UI and rendering.

If your performance is abysmal, take a look at the profiler, or one of the many performance guides out there, I am sure you will learn about things that you had no idea would cause performance drops.

Hope this helps! :smiley: