Day 113: Player Movement Part 1

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!!

--

--

--

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

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

Recommended from Medium

React Tutorial A Step-by-Step Guide(2)

Extending Built-in Elements

Getting a handle on Objects in JavaScript

A very famous Javascript interview question

DOM handleEvent: a cross-platform standard since year 2000

6 Tips to Improve Your Conditional Statements for Better Readability

Python As NodeJS Dev | M. Zubair Saleem

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
Tyler Smallwood

Tyler Smallwood

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

More from Medium

Day 103: Creating A Ledge Grab in Unity Part 1

Simplifying Wave Creation with Custom Attributes in Unity

Creating Fog in Unity

How to Build and Test your Unity Game