Endless Runner Game in Unity3D (Part 1)

Now this is what you have to do to get started on your first Endless Runner so ladies and gentlemen fasten your seat belts as we are about to go running till infinity!

  1. Open Unity and set up a 2D project called Endless Runner.
  2. Download the sprites art from this link: http://www.kenney.nl/assets/platformer-pack-redux
  3. Make a folder named “Art” in asset folder and go to spritesheet_players.png, drag and drop that image in the folder.
  4. Open the image in Sprite Editor but before that change the Sprite Mode to Multiple, Pixels Per Unit to 128, Filter Mode to Point (no filter) and Format to Truecolor, hit Apply.

4

  1. Now open the Sprite Editor and select our little guy, we will name him Pixi, make sure that you got a box around Pixi and set the Pivot to Bottom.

Pixi

6. Now apply the changes and click on the small arrow next to spritesheet_players.png, our Pixi shows up. Now drag Pixi to the scene view.

7. Where will our Pixi stand? Let’s make a platform for him, it may not be the prettiest platform in the world but that’s enough for Pixi to stand on. Make a 3D cube and scale it up to say 15.

8. We are in 2D world and we just created a 3D cube, we don’t need z collision information as our Pixi is flat, so remove the 3D box collider and add Box Collider 2D from Add Component > Physics 2D > Box Collider 2D

1
Your scene view should look like this

9. Just like the cube add a Box Collider 2D to Pixi and add a Rigidbody 2D. Rigidbody 2D gives mass and additional properties to our Pixi like a normal real world object. You can play around with them to know what they do, actually you should really make this a habit of tweaking with the things. We don’t need any rotations of Pixi so got to Constraints and freeze Z rotation.

10. Now it’s time for some coding people! Add up a script in your favorite language (I’ll use C#) to Pixi! name it PlayerController and open it for editing.

11. Pixi must be moving in x direction all the time so we need to give him a velocity in x-axis. Pixi has to be able to jump with a jump button much like a classic Mario jump!

public float moveSpeed;
public float jumpForce;

private Rigidbody2D rb;

void Start () {
rb = GetComponent<Rigidbody2D> ();
}

void Update () {
rb.velocity = new Vector2 (moveSpeed, rb.velocity.y);

if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButton(0))
{
rb.velocity = new Vector2 (rb.velocity.x, jumpForce);
}

}

12. But we have a problem Pixi keeps on jumping even if the lad is not on the ground, this is insane we can’t allow this in our game. So what to do? If only there is a way to identify when Pixi is touching the ground. A bool value maybe with variable name Grounded? Yeah! let’s go with that, so when Pixi touches the ground Grounded changes to true otherwise it remains false.

13. We have already identified the Ground with layer Ground, let’s find a way to reference that. We will make a public LayerMask variable called GetGround and in the inspector panel we will set the GetGround to Ground thereby referencing the Ground layer.

2

public float moveSpeed;
public float jumpForce;
public bool Grounded;
public LayerMask GetGround;

private Rigidbody2D rb;
private Collider2D playerCollider;

void Start () {
rb = GetComponent<Rigidbody2D> ();
playerCollider = GetComponent<Collider2D> ();
}

void Update () {
rb.velocity = new Vector2 (moveSpeed, rb.velocity.y);

Grounded = playerCollider.IsTouchingLayers (GetGround);

if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButton(0))
{
if (Grounded) {
rb.velocity = new Vector2 (rb.velocity.x, jumpForce);
}

}

}

14. Get a reference to Pixi’s collider component using playerCollider variable and setting it up in start(). But how do we know when the Grounded should be set to true i.e. when is Pixi actually touching the ground. For that add this bit of code

Grounded = playerCollider.IsTouchingLayers (GetGround);

Basically IsTouchingLayers method returns a bool type when the playerCollider gets in touch with GetGround layer. Add Grounded as one more condition to jump using if() and there you go! Now Pixi can only jump when he is touching the ground.

3
Here is Pixi paused right in the middle of a jump

So folks! that’s how you get started with a infinite runner game in Unity. Thanks!

 

Advertisements

One thought on “Endless Runner Game in Unity3D (Part 1)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s