Unity new Input System : Smooth Movement (Part VI)

Matteo Lo Piccolo
5 min readDec 30, 2021

--

In this article we setting up a new Input System from scratch (yep, again) and focus to make Player Move smoothly.
We make this because the best method to learn something in many case is Create it, use it, delete it and create it again.
It’s just practice at the end.
Ok, let’s delete everything, and restart.

And open it.

We recreate the Player, and as always delete everything else.

Then, we create a Movement, set Action type to value and Control Type to Vector2.
Then add the 2D Vector Composite

and map WASD as control, then Save Assets

Last step, toogle create C# Class and click apply

P.S. Probably you have the same error as mine, the solution is simply delete the C# Class PlayerActions, I rename the ScriptableObject PlayerActions to PlayerInputActions, and automatically Unity recreate the C# Class. And the error is gone.

Now we create a Player Class, a simple cube in the scene and call it Player, and attach the script to it.

At this point, we have to take a reference to PlayerInputActions

In Start we initialize it

and Enable it

And now we can create the Movement Action

Inside it we create a Debug.Log() with string interpolation to the read value of x and y.

Let’s try it in Unity and see

Everything works fine.
Now, to apply this to our Player, how can we do?
Normally we have something like this

But the fact is : at logic level, nothing change, so we need to read the value, as we did in the Debug.Log(), and pass that value into Translate method.

For example, we can create a variable that store that values, and pass in transform.Translate()

Let’s try it

It works, but not in the way we expected right? This because we have the buttons setting to click them one time, not holding down. So we constantly have to press the buttons to make it moves.

And another thing that we can notice is : in the old system we have Input.GetAxis and Input.GetAxisRaw, if we don’t know the difference is essentially with first we use float and with Raw we use int, so the movemente is like 1, then 2 and then 3 and so on.
In this system seems we have only the Raw axis, and obviously this can be a problem.

There is a solution, which is:
we delete the method and delete the performed section, and in the Update we create “exactly” what we created with Input.GetAxis

Then we take this two lines, cut and paste in the Update, then I make a little change

That’s it.
And if we try it

It works very well, but one question is behind the corner : if we have a 3D game, probably we want to move with A and D left and right, but with W and S forward and backward, not Up and Down.
How can we do it?
Really really simple :
we have a Vector2, but we can use that Vector2 to map a Vector3!
How?

Well, we have Translate method, and inside it we can do this :

Try it again

Perfectly!

And to clean up our code, we can do this :

And now we have “CalculateMovement” in the Update.

--

--

Matteo Lo Piccolo
Matteo Lo Piccolo

Written by Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!

No responses yet