Day 113: Player Movement Part 1

Tyler Smallwood
4 min readMar 28, 2022

--

Objective: Get the player moving with animation.

Before we start on moving, let’s knock out the idle animation. To start we will grab the first frame in our animation and bring into the Hierarchy. We will also drop this under a empty game object. We will keep the sprite object free of to many components. This is to help if we decide later on to change the player sprite.

With Player_Sprite selected we will create our animation. With the animation open we will select create on the animator.

Save the animation file in the Project. I created a folder to hold all animation, and inside the animation created another for the players animations.

All we need to do now is drag in the sprites for the Idle animation. I went ahead and set the sample size to 10. Adjust it to how you would like it.

Now our character has the Idle animation when the game starts. Now to move on to running animation. Let’s go ahead and create a Player script. We will put the script on the Player object. The parent to the sprite object.

Before moving on to the script let’s add a few components to the player. We will need Rigidbody2D to use physics and a box collider to stay on the platforms. In the Rigidbody component, you can freeze the rotation on the z axis. This will prevent the player from rotating.

Now that we have all the components, let’s move to the script. First thing we need is a reference to the rigidbody.

Let’s assign it in start by using GetComponent. We will also null check it.

Now to the moving. In update, we will need to assign our input to a variable. We will create a local variable and assign it to the Input.GetAxisRaw. Using GetAxisRaw takes the float from 0 to 1 as soon as the keys are pushed.

Next will be to assign the rigidbody’s velocity to a new vector 2 with X axis being adjusted. We will use the current y axis of the player in the Y axis. This will stop anything adjusting the Y axis while moving.

Now we can move left and right. But as you can see in the Gif below, our player faces the same direction when moving. Let’s fix that.

To get started with Flipping the sprite to face the right directions we will need to get a reference to our Sprite renderer on our child object.

We’ll do the same as the RigidBody and assign it with Getcomponent and null check it.

Now down to where we get user input we will check to see if our input is greater or less than zero. If it is greater we will face right and if it is less we want to flip and face left. We will set flip on the X axis to false to face right and true to face left.

Let’s make this a little cleaner. If you are using Visual Studio, highlight the if statement, hit alt+enter and extract method.

Now we have a method that checks the horizontal input.

Now our character flips when going right and left respectfully.

Tomorrow we will cover setting up the animation for running. Thank you for reading and have a wonderful day!!

--

--

Tyler Smallwood

I am passonate on learning to program and use Unity to become a skillful Unity Developer