How to Get Simple 2D Player Movement with One Line of Code in Unity

Justen Chong
3 min readMay 6, 2021

--

transform.position = new Vector3(transform.position.x + Input.GetAxisRaw(“Horizontal”), transform.position.y + Input.GetAxisRaw(“Vertical”), 0);

Voila! It’s that simple! You can legitimately just copy this one line of code and put it into the Update function of your Player script and it work. However, depending on how you’ve setup your scene you’re either going WAY TOO FAST or WAY TOO SLOW. Wouldn’t it be nice if there was a way to adjust it?

Well, the first thing you’ll learn about programming is that anything is possible. It’s merely a question of execution.

First, let’s do a breakdown of what’s going on above

transform.position = The coordinates of the object in the game’s world as a Vector3 data type

new Vector3(float, float, float) = You can declare a fresh Vector3 in Unity C# using this syntax. The Vector3 will be set to whatever numbers you put in the brackets as (x,y,z) accordingly. Just make sure they’re floats or ints!

Input.GetAxisRaw(“AxisName”) = This is how we’re getting input from the player. The Axis refers to something in Edit > Project Settings > Input Manager > Axes and returns a value between -1 and 1 inclusive.

So if we were to take a look at what the code is saying it would be something like this:

“Every frame my coordinates will be:
my current x + a number between -1 and 1 inclusive,
my current y + a number between -1 and 1 inclusive,
my current z

Knowing this we can easily slip in a public variable so that we can adjust it in the inspector. Just go back to the top of your code and create a public float:

Here, I’ve initialized it to 8 because I feel that it’s a good starting point for testing out how the movement feels. If you leave it blank then it will initialize to 0, but you can initialize it to anything you want.

Now all we need to do is work this back into the original line of code! Observe:

transform.position = new Vector3(transform.position.x + Input.GetAxisRaw(“Horizontal”) * moveSpeed, transform.position.y + Input.GetAxisRaw(“Vertical”) * moveSpeed, 0);

Now what is the code saying? Something like this:
“Every frame my coordinates will be:
my current x + a number between -1 and 1 inclusive * a number,
my current y + a number between -1 and 1 inclusive * a number,
my current z

Since that number is a public variable we are free to adjust it as we need! It makes for much quicker testing and debugging as well.

One last tip is that it is generally a good idea to also multiply your movement by Time.deltaTime, which is a constant provided by Unity. Talking about a time class will probably be it’s own article so I won’t dive into it now, but our final line of code should look like this:

transform.position = new Vector3(transform.position.x + Input.GetAxisRaw(“Horizontal”) * moveSpeed * Time.deltaTime, transform.position.y + Input.GetAxisRaw(“Vertical”) * moveSpeed * Time.deltaTime, 0);

Just stick that into your update function and make sure you declare moveSpeed at the top as a Global variable and voila! You have 2d movement in Unity.

--

--

No responses yet