Physics Material 2D

In 2D games, the concept of physics becomes a bit more, let's say, linear. By this, we don’t have to worry about a ball bouncing off along the z-axis, or the trajectory of an arrow being dramatically affected by various environmental objects, other than those in plain view. For example, if there is a tree in the path of your character, chances are if you fire an arrow towards an enemy on the other side, the arrow will hit the tree first.

Don't worry if you didn't do too well in physics in school. This isn't a test, but rather an explanation about the physics behind the gameplay in a platformer. It is important to know these things for many reasons. For example, when you're trying to increase the difficulty within a level, it is important to remember and predict how certain elements within the game will behave. Parts of a game, such as an arc that a player jumps, gravity (or lack of), the velocity that a player can run, the amount of friction between the player and the ground beneath them, and so forth, are essential considerations that all have to do with physics.
For this section, we are just going to consider Physics Materials 2D, which is the basic way to set some physics properties to a material.

You can create a Physics2D material with a right-click on the Project panel and then Create | Physics Material 2D. If you double-click on it, you can see its properties in the Inspector. When it comes to Physics2D material in Unity, there are only two parameters to take into consideration:

  • Friction: Indicates the amount of friction; the less the friction, the more slippery the material. Try to create a new material with zero friction, then apply it to the terrain; you will end up with something very similar to ice.
  • Bounciness: Indicates how much energy the material gives back when something hits the material. Try to create a new material with Bounciness to 1, and apply it to any sprite, and let it fall to the ground; you will see the sprite bounce. An example of this is shown as follows:

As far as the game we are creating goes, you can try to experiment with different materials for the different surfaces of your game. However, keep in mind that whether you're trying to outrun an enemy or beat the countdown to a massive explosion, sliding allows you to neatly glide across a large amount of space faster than you could run it. As a result, you can use this game mechanic if you wish.

Since our character will collide with the ground, we need to properly set it up.

First of all, as exported from Tiled2Unity, the Terrain comes already with a collider, which is great. However, we need to set a new layer for it, so when we do some checks to allow our character to jump, our system will go flawlessly.

Navigate to Edit | Project Settings | Tags and Layers. This is the screen that will open in the Inspector:

Open the Layers menu, and add a new one called Ground, as shown in this next screenshot:

Also, later we will need a tag for implementing Jump Pads. Since we are already in the right menu, open the Tag menu, and add a JumpPad tag, as shown in the following screenshot:

Finally, select our Terrain in the Hierarchy panel, and apply the Ground layer on it, like in the following screenshot:

If it asks you to apply the change to all the children (like in the following screenshot), then click Yes, change children:

Now, our terrain is good to go, and it's time bring our hero to life in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset