PLayer Movement

Unity Game Development – 05 – Player Movement

In order to make the player move , we need to add a script to the Player GameObject, that takes care of the input from the user.

1. Getting player Input

The Input.GetAxisRaw(“Horizontal”) returns any movement values make by the user horizontally. This can be used to control the GameObject by setting its velocity appropriately in the relevant direction.

We can also get the other keys that were pressed, for example… Input.GetKeyDown(KeyCode.Space) checks if the Space bar was pressed. Using the combination of these 2 fields, we are able to determine if the user has moved vertically or horizontally.

2. Controlling Player Animations

As we have created several animation states previously, we can now use them to control the display on the screen.

We can Access the properties within the animation using the SetFloat , SetBool etc methods. This method allows transitioning between states , For example, to change the state from Player to PlayerRun, we can use animator.SetFloat("speed", 5f); Here the speed property is used as the condition to move from Player to PlayerRun.

3. Collision Detection

Set up a TileMap Collider and name the layer as “Foreground” . The layer number is 8 for the selected Layer. So now all the surfaces on the foreground layer are surfaces.

The player also has Rigidbody2D and a 2d Collider on it. As long as the gravity of the Foreground layer elements is set to 0, the player will be able to stand on the layer surfaces.

4. Raycasting

In order to make a jump , we first need to check if the player is already jumping or not. In order to determine this, we can create a RayCastHit2D and check if it has collided with objects on the Foreground layer number 8.. If the Ray has hit a target, it will return this information about the target that was hit.

To do this, use RaycastHit2D hitInfo= Physics2D.Raycast(transform.position, Vector2.down, 0.6f, 1 << 8);

5. Physics Materials

Apart from the other movement related tasks, we also encounter an issue at the edges, where the sprites tend to get stuck. IF we create a new Physics2D material and set the “Friction” to 0 , it will eliminate this error.

The player appears during running like shown below…

Below is the Idle Animation, where player has slight movements

And finally, this is where the plater jumps…

Unity Game Development – 04 – Player Animation

Platformer games usually have lot of characters on them. The Hero, the enemies, some NPCs etc. IN order to really use a character, we need to have the appropriate animations on it . Let’s take Player movement as an example. If the player is not used, it should be in idle animation, the player should be able to run , jump , dive, fight …etc.

In this post, we will take an existing chaacter, and see how we can add animations to the character in unity.

1.Importing Spritesheets and splitting sprites

Characters usually come in Spritesheets. And since there are a lot of open source spritesheets on the internet, i just happened to get my hands on one..

As you can see , We have a spritesheet with different player movements available.

We can use the SpriteEditor and slice this up into the component Sprites. It should now appear like this with the individual sprites …

2. Creating Animations.

Select the first 3 sprites and drag that on to the Scene. A window will popup , asking to create a new Animation. Name it appropriately and save.

You will see a Controller and an Animation created. The controller is shown below. It contains several states, and since it is a new one, it contains current “Idle” state as well as the default Entry, Exit and AnyState

The Animation looks like below.. For the selected “Sprite” , it shows the 3 frames that were selected, and also shows the frame rate per second this animation plays. We can tweak with these settings through the editor. Each of the three frames is a keyframe . We can drag other sprites on to the empty states to make various animations.

3. Creating Other Animations

We can create states for the other movements and add Animations to those.

I have created a new Empty State called “Run” . We can add transitions between 2 states. It will create directional arrows that connect states.

After adding transitions, we can even control when the transitions should occur.

4. Transitioning between States

We can create some parameters to control the transitions, for example an integer flag that controls whether the state should be run or idle.
In the below step, upon selecting the transition, we can see the properties of the transition on the right. Once the parameters are created, they can be added in the Conditons section in the Inspector.

This is how the final demo player Animation controller looks like …

The below is an example of how the player’s state changes . Currently it shows the player running motion.

In the next post, we will see how to use these Animations in the Game

Unity Game Development – 03 – Prefab Brushes and Animated Tiles

What if painting a sprite on to the tilemap was not enough ? What if you had to paint Prefabs ? For example, if you had a collectible that also has animation and needs to be present in several places in your level.

Yes , that’s right . This Prefab Brush is perfect for painting prefabs on to your Tilemap. Now you don’t have to manually drag the prefabs all over the place. You can simply change the default brush and create the prefab

1. Install Prefab brush from Github

Navigate to this url https://github.com/Unity-Technologies/2d-extras and download the zip file containing all the Tilemap related extras.

After unzipping, place the below items into your assets folder

2.Create Prefab to use with the brush

Import a coin sprite, and create a Prefabs folder,and create a new Prefab called coin.

Now when you try to create a game object in your project, you will see the brushes appearing in the Context menu.

Create new Prefab Brush

Next , Lets create a new Prefab brush, and in the Inspector, set the Prefabs Size to 1. It will ask to select a prefab as template, and you can select the coin prefab.

Set prefab in PrefabBrush

3. Selecting new Brush and Paining

In the Tile Panel, at the bottom left side, there is a dropdown to select brushes to paint. You will find the coin brush there. By selecting that , you will be able to paint coins on to the Tilemap

Newly painted prefabs using Prefab Brush

4. Creating Animated Tiles

4. If you notice the content menu, the Animated Tiles are also added when we imported the extras from Github. Let’s create an Animated Tile called CoinShine

In the Sprites folder, i already have 10 Sprites for the Coin Shine Animation. We need to set the number of frames on the Animated Tile , and individually select each frame.

5. Set Sprites in Animated Tile

6. Set speed and import Tile into Tile Palette

After Selecting the sprites, also set the minimum speed to a value higher than 1. I just set it to 30 . You can now drag this CoinShine Tile into the Foreground Palette like any other ordinary sprite.

Using the brush tool and the new Animated Tile in the palette, we can paint it on to the Tilemap.

Painting Animated Tile

Hope you have fun creating animated collectibles from now on. Also, you could place all the collectibles on the Collectibles Tilemap instead of the Foreground Tilemap, that way you will have greater flexibility in using it.

TileMap Colliders

Unity Game Development – 02 – Tilemaps Colliders

In order to let the player interact with the environment created in the Tilemap, we need to add colliders . For example, if the player needs to walk on the ground and not fall through, then the entire ground assets need to have a collider. Tilemap colliders simplify this process . Let’s dive in and see how to use this feature.

1. Create scene with Tilemaps

This is the new Scene that I created , having 2 Tilemaps, Foreground and Background. The Inca stone blocks are all on the Background Tilemap , and the remaining elements are on the Foreground Tilemap

Foreground and Background tilemaps

2. Setting up Tilemap Collider

Select the background Tilemap, and in the Inspector panel add a new Component called Tilemap Collider. You will be able to see the colliders on all Tile Assets in the Tilemap , highlighted in green.

Add Tilemap Collider Component
Assets highlighted with colliders

3. Remove unneeded colliders using Composite Collider 2D

If you notice above, there are colliders separating each individual tile in the background Tilemap. This is rather unhelpful, as we only need the colliders on the surface. To achieve this , we will need to add a Composite Collider 2D component

Of course, in case of games like Pong, the individual colliders would be most helpful, but for our scenario, we would only like to see the collider on the surface.

Lets go ahead and add a new Composite Collider 2D to the Background Tilemap . It creates 2 components – one Rigidbody 2D and one Composite Collider 2D

4. Configuring the Tilemap to use the Composite Collider

In the Tilemap Collider 2D, we need to select the check box “Used By Composite” . This will instantly transform the collider on the background tilemap into the uniform collider on the surface.

Also, we can set the Rigidbody 2D Body type to “Static” , since we are configuring the floor .

Used by Composite checkbox is set
Uniform collider on surface of Tilemap

And that’s it, you should be good to go . Makes life so much easier than manually creating the colliders one by one for surfaces.

Happy level designing !

Unity Game Development – 01- Working with Tilemaps

Tilemaps are a great way to design levels in Unity. They are extremely useful in separating content in layers . It is very simple to use a Tilemap in unity. In this post, we will explore how to create and use Tilemaps to create amazing levels for 2D games.

In combination with the Tile Palette, which holds TileAssets, this is a truly comfortable features in designing levels.

1. Create a new Tilemap GameObject

Create Tilemap gameobject in Unity

After adding a tilemap to the screen, you will immediately see a Grid in the Scene panel. In order to use the Tilemap , we need to setup the TilePanel .

2. Set up the Tile Palette Window

Display TilePalette window in Unity

3. Create a new Palette

Inside the tile palette window, you see an option to Create a new Palette. We can create Multiple Palettes . For example , Background palette to store all sprites related to background layer.

Create new Tile Palette

4. Create TileAssets using Sprites

You can use your existing Sprites to create Tile Assets. Simply drag them into the BackgroundPalette that we just created, and it will create the corresponding Tile Assets.

Sprite that will become Tile Asset

By dragging the Parent sprite, all the slices will be imported and are available individually.

Tile Assets created in Tile Palette

5. Painting Tile Assets on Tile Map

Now you can paint the TileMap with the assets present here. Using the Paintbrush tool in the TilePalette, we can draw the TileAssets on to the TileMap.

6. Creating a second TileMap

Lets do step 1 to 5 again for a new Tilemap. We’ll call it the Foreground Tilemap, and start painting from the Foreground Palette on to it.

  • Lets create a second TileMap called Foreground.
  • Also create a new Palette called ForegroundPalette.
  • Import a new Parent sprite into the ForegroundPalette.

I have selected the “Focus on” setting in the Scene editor to “Tilemap” which focuses on the currently active Tilemap

Using multiple Tilemaps in Unity

References