Simple Player Movement in Unity

Pavel G Rodriguez Garcia
3 min readOct 5, 2022

In this article, we will look into how to get a player moving to the left, right, up, and down using the WASD keys.

The setup I have here is a cube that will act as our player, with a simple player script attached to it. Once we have the cube in place, how do we control to move it throughout the screen as in this example, but with only using the keys on our keyboard?

To begin manipulating movement through code, we have to access the Transform module, and change the values in the Position of the object.

To get a good idea as to what the Transform module does, we can access the Unity documentation by clicking on the question mark on the top right of the Transform module.

Code examples can be obtained by clicking on the top of the page for Scripting API and searching for Transform.

Under the public methods there is a list of methods we can call with a description of what they do.

The one we are interested in is the Translate method.

Clicking on it will take you to some code examples.

What we are can take away from the code examples is the syntax of the code, so we first need to type “transform.Translate(code goes here);”. Jumping into our player script that is attached to the Player and type in our code syntax into the Update method. That will bring up a tool tip that will give us clues as to what to do next.

The clue we got from the tool tip is that a Vector3 has to be used for the code, and if we use “Vector3.right” this signifies we want to update the position of our cube to the right by one every frame. Depending on how fast your machine is, this could be getting updated up to hundreds of times per second causing our cube to fly to the right!

Obviously this is not the behavior we want, but this is just to illustrate that even though the cube is getting update once per frame, the Update method is called every frame and thus making the cube be constantly moving.

Be on the lookout for part 2 .

--

--