# [Unity2D] Moving your player sprite

Information
Step 1
In this lesson we are going to learn how to move our player with the Left and Right arrow keys! (Or A,D key)
Step 2
First let's create a floor. First we need a picture of a white square. Save it and import it to your project.
Step 3
Create a new 2D sprite.

#### Name it as floor.

Step 4
We'll use the White image as our floor. Click and drag it into our Floor sprite.

#### Let's move the floor down under our player and enlarge it!

Step 5
Step 6
Give your Player a BoxCollider2D and a RigidBody2D.
Step 7
Press Play and your player should have gravity and fall on top of the floor!
Step 8
Create a new C# Script in your project panel. Name this as Player.
Step 9
Open the Player.cs Script and type this code into your script.
Information
Step 10
Read this to understand how we will move the player.

### What are the values of horizontal?

• When you press Left or A, horizontal becomes -1
• When you press Right or D, horizontal becomes 1
• When you don't press anything, horizontal becomes 0
Step 11
Go back to Unity. Attach the Player.cs script to your player and give it a speed of 5 (or more).
Step 12
Now click play and press LEFT arrow key, RIGHT arrow key, or A and D keys to move the player left and right!
Step 13
The Camera view is a little too small, let's increase the size from 5 to 10.
Challenge
Step 14
Can you try moving the other Sprite with different keys?

#### HINT:

1. We make another script for the other sprite.
2. Instead of saying: rb.velocity = new Vector2(horizontal * speed, rb.velocity.y); We don't use "horizontal", It's either a -1, 0, or 1.
3. Instead of Input.GetAxis("Horizontal"), we use Input.GetKeyDown(" ").
Information
Step 15
Next we'll learn how to flip the way the character is facing when you press left and right! Let's open up our Player.cs.
Planning
Step 16
What can we do to make the player sprite flip?

#### For let's keep the Scale X at 1. Don't change it.

Planning
Step 17
What direction are all our sprites facing right now? The left or the right?

#### They are always facing the right!! Keep this in mind for the next step!

Challenge
Step 18
Let's open up our Player.cs script. And type this new part in. A bool is a variable that can only be true or false.

#### What is facingRight equal to?

• A bool can only be true or false.
• In step 17, what direction is all our sprites facing?
Challenge
Step 19
Information
Step 20
Here is what the Flip function does, first we check when we need to flip, then we reverse the player's x scale!!
Step 21
Save your script, go back to Unity, and hit play to see if this works!
Planning
Step 22
If you go off a cliff or travel too fast, your player will start to rotate and fall... Lets fix this!
Step 23
Simply select your player. In the Rigidbody2D component, click Constraints and check Freeze Rotation Z
Step 24
Check again to see if that worked!
Step 25
You are finished with this lesson! Click Submit and continue onto the next lesson!
[Unity2D] Moving your player sprite Info

Account

MVCode Clubs

Created By

Course:

2D Platformer Game

Access Level