Objective #1: Convert Player Movement
In this article I’ll go over my process for converting player movement from the legacy input system to the new input system in Unity. If you’ve made it this far, surely you have an old project or 2 with the legacy input system driving all your inputs. Open it up, import the new input system into the game, leave both input systems enabled for now and when we’re done we’re going to turn off the old input system! Lets go!
I’ve opened a project that currently uses the old input system. My first step is to convert the player movement to the new input system. To do this, understanding the current logic is key!
In my Player
class I’ve got some pretty basic movement set up.
As you can see, we’re using the Character Controller component on the player object. We are rotating with input from our horizontal axis and moving forward and back with input from our vertical axis. Beautiful. What I want to do is make all the same inputs do the exact same thing. I’m gonna spice it up a little and add some controller support. This got me thinking though… what if the right stick rotates the player and the left stick moves them back and forth… ya know… like most games ;)
I’m not going to go into the basics of setting up input actions, bindings, etc. We’ve hopefully got a decent grasp on that right now. So for now I’m gonna make a Player Action Map, make a movement action, and assign the bindings.
I’ve made an action for Movement (forward and Back) and an action for rotation (left and right). These are value action types with an axis control type.
To start changing this to the new input system, we obviously need to use the InputSystem namespace.
In Start() I’ve enabled the Player Action Map.
Now I can start setting up my logic for what inputs control what.
For things that are handled in Update(), I like to poll instead of set up the callbacks.
I’ve changed the CalculateMovement() to support input readings using the new input system!
As you can see, I set up 2 variables. 1 for movement and 1 for rotation and I have those variables assigned to the ReadValue of those actions. For 1-D Axis bindings you have to use a float value.
Transform.Rotate is being driven by the rot
variable and the movement is being driven by newly implemented direction and velocity variables. Easy peezy! To set up controller support, I just added new bindings to those actions.
Final result: