# About creating a platform game on Unity. Part One, Characteristic

• Tutorial
Hello, Habr!

We all here love high-quality step-by-step guides for beginners , which is a sin to hide. There are a lot of manuals on Unity platformers, but quality ones aren’t very good. Today we will try to add another lesson to the piggy bank of good lessons. And we will create a character for a 2D platforming game, no more, no less.

Join, learn, but remember: there are a lot of gifs under the cut.

So where to start character creation? Of course, from his sprite. Just drag the picture from any folder on your hard drive to the Assets tab, and from there - right onto the stage. I will make a reservation right away, in this lesson we will not consider the creation of animation. But in the next parts of the cycle - we will.

As you can see, after adding to the scene (let's agree that this is a full-fledged analogue to the phrase scene view), the sprite has two components. The first is transform. He is responsible for the location of any game object on the stage, its scale and current angle of rotation relative to the axes. The sprite renderer component is just involved in rendering the sprite of our charming nosach in the process of editing and playing.

But while the character is just a picture, he cannot interact with the world around him. For this, components hidden in the physics2d tab come to the rescue. In our case, these are box collider and circle collider. We will add the first to the upper part of the character for collisions with the walls and everything else, and the second we will place at the level of the legs. This will allow you to move on inclined surfaces (and surfaces with a small difference in elevation) without any problems.

Now, for gravity to act on the hero, let's add him rigidbody2D (well, or a solid, as you like). To do this, everything in the same menu add component -> physics2D, select the item rigidbody2D and another component will be immediately added to the sprite.

In short:
mass is clear for what;
linear drag and angular drag - linear and angular drag, respectively;
gravity scale - gravity coefficient for this particular object;
fixed angle disables the coup of a character in a collision with something, say, flying;
isKinematic fixes an object once and for all at one point;
interpolate sets the smoothing mode when rendering the character;

For the platformer, the standard gravity value (-9.81 along the Y axis) is not very suitable, so change it to some magic number. For example, -30. To do this, go to Edit - Project Settings - Physics2D and replace the desired value in the inspector.

At this point, let's stop adding components to the character. But let's make some of it. In order for us not to have to add the same components a thousand times at each, say, level, Unity has a mechanic called prefabs. It allows you to create a "blank" from the game object, which you can use many times and change as you wish, without fear for the fact that some instances may not change. To create a prefab, simply drag an object from the hierarchy to the assets tab. In my case, also in the prefabs folder.

As you can see, both instances of the hero are beautifully created and exist together. Now we need to create a platform on which our big-nosed friend will stand. Drag the sprite onto the scene and add a polygon collider to it. The dimensions of this collider, by the way, can be changed by dragging its vertices with the shift button held down.

It's time to teach the character to move. Drag and drop the prepared script (it needs to be downloaded and saved to the assets folder) directly onto the character in the hierarchy view.

This piece of code has a number of parameters that are responsible for the behavior of the hero in the game.

maxSpeed ​​and JumpForce - maximum speed (horizontal) and jump strength. It has been experimentally verified that for mass equal to unity and gravity -30, the values ​​maxSpeed ​​= 10 and jumpForce = 700 are optimal.

groundCheck - a child object located at the bottom of the sprite and responsible for determining whether something that we are managing is on the ground

whatIsGround - which, in fact, is considered to be ground. In our case, “ground” is considered everything except the character

groundRadius - a certain value within which a collision with the surface is checked.

As a result, the line grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround); checks if the groundCheck intersects the surface with a radius of groundRadius and, for example, prohibits jumping if necessary.

Finally, let's run the game and see what happened today. To do this, press the Play button in the top ... C'mon, you yourself saw it :)

The hero moves perfectly (although perhaps not quite perfectly), and this creates excellent conditions for us to continue creating a platforming game.

And in the next part, we will figure out how to control the camera, find out who needs to light stars and create the very first enemy. And yes, we’ll fix one critical bug along the way, carelessly skipped in the article during this lesson. Guess which one?

If this article suddenly prompted you to write your own game, then do not waste it! A contest is taking place right now , and becoming a participant in it is too simple: just register , post your game in the store and wait for the results to be announced. And the winners are waiting for the Xbox One and the magnificent Lumia 930!

I'm going to finish my Angry Flappy Swompy 3 Deluxe. Stay tuned, the second part of the article in the coming days!

ps
Platform.png
Character.png (many thanks to Anastasia Garan for rendering the hero)

And here are our other articles on similar topics :