Join Jesse Freeman for an in-depth discussion in this video Setting up the first character, part of Advanced Unity 2D: Platformer Player Movement.
- So now that we have the foundation of our input, it's time for us to look into building a basic player that we'll use for the rest of the course. In order to do this, let's create a new scene, and we're going to save this scene and call it PlayerStaging. The next thing we're gonna need to do is to drag in all the artwork for our project. Simply drag the Artwork folder into the Assets folder, and all the artwork will be imported. Once you have the artwork in, open up the Artwork folder and select all the files.
Another way you can do this is by filtering out just for textures and selecting all. Now before we move on, we're gonna need to do a few things in order to clean this up. The first thing we're gonna want to do is convert the Pixels Per Unit to one. This means that each unit inside of Unity will now be one pixel. A lot of this artwork is very small, mostly 16 by 16, so we want it to be full sized inside of the IDE, and also when we're running our game. The next thing we're gonna want to do is change the Filter Mode to Point.
Since it's pixel art, we want the pixels to be Sharp. The last thing we're gonna do is change the Format to True Color and hit Apply. This should make the changes to all of the artwork in our game. Now let's create the basic walk animation for our player. Scroll down and look for player normal walk 1 and 2. We'll select these two sprites and drag them into the scene. We're also gonna want to create a new folder called Animations. In this, we're gonna create a new animation called PlayerNormalWalkAnimation.
Now you'll notice the artwork is incredibly large. That's because you'll see here that the player's eye is one pixel, and this is roughly the exact same size as one unit inside of Unity. If we scroll out, we'll be able to see the full player artwork. Now in this course, we're not gonna really focus on camera that much. So we're just gonna fix the camera to a size that makes it convenient for us to actually view what's going on in the game. We can select our camera, and let's just change the Scale to 80. If it's a little bit too small, you can always change it to whatever you feel comfortable with.
If you want it to be bigger, you can make it 40. The reason I choose 80 and farther back is because when we start moving around, we want to see more of the scene. And also the reason why I have the Saved tab saved up here and the Game tab down here is that I can preview what's going on in both and not have to worry about the camera. So in the Games tab, we're gonna actually see what's going on in the game, but in the scene, I'm free to move around the camera and take a look at how things are actually gonna look as I'm testing them out.
Now before we move on, we want to actually first test that this animation works. So let's run it, and we'll see that our player runs. But the default state is going to be idle. Now since you can't drag a single sprite down in order to create an animation, we're gonna go in and in Window open up the Animation panel. Let's dock this down here to our Game window. The reason I do this is so that I like to work in the scene view and also have access to the animation.
Now let's rename this game object Player and save it. With Player selected and the animation window open, we'll see the two frames that make up the actual player. We can create a new animation by clicking here and selecting Create New Clip. Again, the window's gonna pop up asking us to save the animation, and we're gonna call this PlayerNormalIdleAnimation and hit save. Now there's no artwork for this animation.
You'll notice the sample rate is also defaulted to 60. We can set this to 12 so everything is consistent with what our original walk animation was set up. We'll drag our idle animation over. Now there's only one frame, so there's nothing to really preview. We'll hit Save. You'll notice that whenever the IDE makes a change to the animation, you'll get red highlighted here and red highlighted in the Play button telling you that you need to change those animations. The next thing we're gonna want to do is open up the Animator.
If you to Window, Animator, and drag the Animator down next to the Animation tab, you'll see that we now have our two states- our PlayerNormalWalkAnimation and our PlayerNormalIdleAnimation. In order to scroll through this window, you can use the scroll wheel on your mouse to move left and right, or hold down shift to move up and down. In Unity 5, we now have a new entry state. An entry state simply marks where the animation begins from. In this case, we just wanna change the NormalIdleAnimation to be default.
So let's right click on this and make this the default layer by selecting Set as Layer Default State. Now if we run our game, you'll see that the player is standing. When we stop, they're gonna return back to the first frame of the run animation because that's how the animation was set up. If we select the Player, we can change the default sprite in the sprite renderer by simply dragging the PlayerNormalIdle over to it. This is a good technique to know if you're working in a large scene or wanna give your game objects custom artwork.
In this case, you can drag a specific sprite over to the sprite renderer, but when the game starts, it'll be replaced by whatever we set the default animation to. At this point, let's save our scene, and then we can start working on the actual level itself that our player is gonna walk around in.
- Building an input manager for keyboard and controllers
- Setting up the player
- Building reusable, self contained, and modular behaviors scripts
- Adding behaviors: walking, running, and jumping
- Managing animation states and player collisions
- Adding a ducking animation
- Detecting wall collisions
- Sticking to, sliding down, and jumping off walls
- Picking up items
- Firing projectiles