Journal 95 — Unity 2D Mobile, Dungeon Escape Android and Player Setup

Objective: To start a clean setup of a 2D mobile game for Android and add the player movement and jump behavior

In the previous articles, I went over the initial 2D Tilemap features. In this article, I will go over the first setup of the project in Android and also the player setup using the Dungeon Escape assets.

First, we need to change the build to Android. This is under File → Build Settings. Select Android and then “Switch Platform”.

After this, we want to set the Game view to 16:9 Landscape.

To make sure the Main Camera looks correct in the Game view, check that the camera Projection is set to Perspective. As you move the Main Camera, you can see a parallax effect between the different grids set for background and foreground. See the adjustments in the following snippet.

Player GameObject Setup

We have several sprite sheets provided for the different player animations that we first need to make sure the Texture Type is Sprite (2D and UI), the Sprite Mode is Multiple, and using the Sprite Editor the sprite sheets are automatically sliced.

Let’s first set up the player in the Scene view with one of the idle animations. After dragging in, we can create an Empty Game Object called “Player”, and put the player sprite as a child called “Sprite”. We want to change the order in layer to 50 to ensure the player always shows in the front of all other objects, and reposition the Player object so it appears the player is walking on the surface.

Player Movement

Now let’s add a C# Player script to handle player movement. For this game, we will use the physics system using RigidBody2D. So for the Player game object, let’s add a BoxCollider2D and RigidBody2D component.

In the Player C# script, we can reference the RigidBody2D component, and modify with the RigidBody2D velocity using the horizontal movement times a speed variable.

We can see the results.

Player Jump

Next we can add a jump behavior and modify the RigidBody2D velocity in the y-direction with a jump force variable. We can use a grounded bool variable to check that the player is grounded.

Next, we can use Physics2D.Raycast to confirm when the player jumps, that the player contacts the ground before being able to jump again. We can also use Physics.DrawRay and Debug.Log statement to verify what the player makes contact with.

As you can see, the raycast hits the player collider, so we need to make an exception so the raycast only registers the collision with the ground layer. This can be done by assigning a “Ground” layer to the Floor game object. In this case I used Layer 8.

Here is the modified code to add the LayerMask. The code “1 << 8” is called a bit shifting. We can add a layer mask by defining a LayerMask variable and adding this in the Inspector.

Fix Ground Check

One more update is to make sure the grounded check accurately changes between true and false so the player jumps correctly. Since the Update method is happening so fast to change the grounded check between true and false, we can add a brief coroutine to pause for 0.1 seconds.

Player Movement Option 2

Let’s modify the player movement script slightly and modularize the movement and jumping and ground check. We can start with a void function for “Movement”.

Then we can modify this further by removing the DrawRay function and a separate grounded bool variable and change this to a return type bool to check the RaycastHit2D with the ground layer.

Thank you for your time!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Chris Nielsen

Chris Nielsen

An Engineering Manager consultant who is seeking additional skills using Unity 3D for game and application development.