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

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.




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

‘DRY’ at Work - Automating the boring stuffs

[hsb]Team Harambae Presents: Rootcon 12 CTF Update + CryForBin 2,3,4,6,7,8 Write-Up

Deploying DotNet Core Application using Kubernetes

Hello HNGi8

How to Parse the Counts section of the Quantum Circuit Results

What is that HDFS (Hadoop Distributed File System)?

Unity Wheel Collider

4 Great Things About Microsoft Windows Server 2022

Microsoft Windows Server 2022

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Justen Chong

Justen Chong

More from Medium

My journey becoming a Unity game developer: 2.5D Infinite Runner-Ledge Grab System-Pt3

Cleaning up the Homing Missile code

P1: Creating a Gritty Office Den

Hidden Shortcuts to Make Level Design Easier in Unity.