Daily progression — getting the player to move.

Esteban Ibarra
3 min readJul 25, 2021

Today we write a script to get our player to move around but not like before using transform.translate, we’re going to be accessing the players rigidbody and manipulate that.

After creating a Player script, we create a handle for the rigidbody:

Next we want to figure out how to move the players rigidbody so we check out Unitys page, we want to affect its velocity and rigidbody has a velocity method so let’s check that out

Scrolling down and looking at the example, we see that if we want to affect the velocity, we give it a new Vector2, and since we want to move left and right, let’s stick the horizontal axis version of the GetAxis input:

We don’t need to affect the y axis (up and down) just yet, so let’s just put the current value in while putting the Input.GetAxis(“Horizontal”) for the x value.

Now let’s see if we got things moving:

The player is moving, but very slow! Let’s include a speed variable:

Now let’s multiply the horizontal axis with the speed. While we’re at it, let’s put the movement check into its own function.

Also, for better readability, let’s put the Input.GetAxis into its own variable, _horizontalInput:

Yeah, that’s better!

Just noticed there’s a slight issue when the player reaches the edge…

The player sprite rotates when it reaches an edge. Let’s just click on Freeze rotation Z in the players rigidbody options.

Ok, the players no longer spinning, great!

One more thing, The sliding around with friction is good, but we want tighter controls. The reason the sliding is happening is because we’re using GetAxis which will return a floating number, but if we use GetAxisRaw, we’ll get a more binary 1,0,-1 response for a crisper feeling control.

So let’s see the difference:

Much Tighter!

--

--