Tag Archives: design

Creating animations for the player sprite in a 2D Unity game

In this tutorial, you will learn how to create idle, walk/run, and jump animations for your 2D player in Unity. In the next tutorial we will set up the player animation controller.

Next tutorial: Setting up the player animation controller

Jumping in a 2D game with ground check in Unity

In this tutorial, you will learn how to write a script in C# code that allows your player to jump only when they are touching the ground.

Watch the video below and scroll down to view the sample code.

Sample code

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

  public float speed = 5f;
  public float jumpSpeed = 8f;
  private float movement = 0f;
  private Rigidbody2D rigidBody;
  public Transform groundCheckPoint;
  public float groundCheckRadius;
  public LayerMask groundLayer;
  private bool isTouchingGround;

  // Use this for initialization
  void Start () {
    rigidBody = GetComponent<Rigidbody2D> ();
  }
  
  // Update is called once per frame
  void Update () {
    isTouchingGround = Physics2D.OverlapCircle (groundCheckPoint.position, groundCheckRadius, groundLayer);
    movement = Input.GetAxis ("Horizontal");
    if (movement > 0f) {
      rigidBody.velocity = new Vector2 (movement * speed, rigidBody.velocity.y);
    }
    else if (movement < 0f) {
      rigidBody.velocity = new Vector2 (movement * speed, rigidBody.velocity.y);
    } 
    else {
      rigidBody.velocity = new Vector2 (0,rigidBody.velocity.y);
    }

    if(Input.GetButtonDown ("Jump") && isTouchingGround){
      rigidBody.velocity = new Vector2(rigidBody.velocity.x,jumpSpeed);
    }
  }
}

Next tutorial: Changing the player sprite

Making the player jump in a 2D game with Unity and C# code

In this tutorial you will learn how to write a script to make your player jump in a 2D game with C# code in Unity. In the following tutorial, you will learn how to make the player jump only when they are touching the ground.

Watch the video below and scroll down to view the sample code.

Sample code

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

  public float speed = 5f;
  public float jumpSpeed = 8f;
  private float movement = 0f;
  private Rigidbody2D rigidBody;

  // Use this for initialization
  void Start () {
    rigidBody = GetComponent<Rigidbody2D> ();
  }
  
  // Update is called once per frame
  void Update () {
    movement = Input.GetAxis ("Horizontal");
    if (movement > 0f) {
      rigidBody.velocity = new Vector2 (movement * speed, rigidBody.velocity.y);
    }
    else if (movement < 0f) {
      rigidBody.velocity = new Vector2 (movement * speed, rigidBody.velocity.y);
    } 
    else {
      rigidBody.velocity = new Vector2 (0,rigidBody.velocity.y);
    }

    if(Input.GetButtonDown ("Jump")){
      rigidBody.velocity = new Vector2(rigidBody.velocity.x,jumpSpeed);
    }
  }
}

Next tutorial:  Jumping with ground check

Moving the player in a 2D game with Unity and C# code

This video explains how to move your player in a 2D game in Unity with C# code. After watching this video, you will be able to move your player left and right in a scene using keyboard input.

Watch the video below and scroll down to see the sample code.

Sample code

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour {

  public float speed = 5f;
  private float movement = 0f;
  private Rigidbody2D rigidBody;

  // Use this for initialization
  void Start () {
    rigidBody = GetComponent<Rigidbody2D> ();
  }
  
  // Update is called once per frame
  void Update () {
    movement = Input.GetAxis ("Horizontal");
    if (movement > 0f) {
      rigidBody.velocity = new Vector2 (movement * speed, rigidBody.velocity.y);
    }
    else if (movement < 0f) {
      rigidBody.velocity = new Vector2 (movement * speed, rigidBody.velocity.y);
    } 
    else {
      rigidBody.velocity = new Vector2 (0,rigidBody.velocity.y);
    }
  }
}

Next tutorial: Making the player jump with C# code

Organising and grouping assets in the hierarchy in Unity

This tutorial explains how to organise and group assets on the hierarchy in your 2D game as well as the parent-child relationship between assets on the hierarchy.

Next tutorial: Introduction to C# scripting and collision detection

Introduction to C# scripting & collision detection for 2D games in Unity

This video gives an introduction to writing code for 2D games in Unity using the C# programming language. We’ll get started by adding collision detection so that our player object (or character) can collect points when colliding with coins in the game. You’ll learn how to setup 2D collision detection, increase a score variable, display messages in the console to test your code, and how to destroy objects (or make objects disappear) in a scene using code.

Watch the video below and scroll down to see the sample code.

Sample code

using UnityEngine;
using System.Collections;

public class CoinScript : MonoBehaviour {

  public int score = 0;

  // Use this for initialization
  void Start () {
  
  }
  
  // Update is called once per frame
  void Update () {
  
  }

  void OnTriggerEnter2D(Collider2D other){
    Destroy (gameObject);
    score++;
    Debug.Log ("Score: " + score);
  }
}

Next tutorial: Moving the player with C# code

Using Sorting Layers to arrange assets in Unity 2D game design

This tutorial explains how to arrange assets in a 2D scene in Unity using Sorting Layers. If you have assets that you want in the background and assets that you want in the midground and foreground of a scene, then you can use Sorting Layers to arrange these assets. You can also use Sorting Layers to fix the problem of disappearing assets in your scene.

Next tutorial: Organising and grouping assets in the hierarchy

Physics2D materials and more 2D colliders in Unity

This video explains how to use Physics2D materials in Unity to add properties such as bounciness or friction to objects in your 2D game scene. We will also look at more 2D colliders in Unity including the box collider and polygon collider.

Next tutorial: More Physics2D materials

Adding physics and colliders to your 2D game in Unity

This video explains how to add physics and colliders to objects in a 2D game using Unity. In this tutorial, we will look at how to add the RigidBody2D component to an object in the game scene, as well as edge and circle colliders.

Next tutorial: Physics2D Materials and more 2D colliders

Adding assets to your 2D game in Unity

This video explains how to import assets into a 2D game project in Unity. Assets in your game will include items such as background images, character sprites, objects in your game scene, materials, and scripts that will control the game.

We will get started by adding a sky background, the ground, and a player object (ball). To add assets to your game, you simply drag and drop files directly into the Assets panel. Files you might like to add include images for backgrounds, spritesheets for characters, animations, sounds, and materials.

addassets

To see how this is done, watch the video below.

Next tutorial: Adding physics and colliders to your 2D game

Introduction to 2D Game Design in Unity

Welcome to the 2D Game Design in Unity tutorial series. This first video in the series explains how to download the Unity software and set up your first 2D game project. You can download the Unity software from here and get started for free. This tutorial includes a step-by-step video (scroll down to watch).

After downloading and installing the Unity software on Mac or Windows, you can open Unity and create a new project.

Screen Shot 2017-04-14 at 2.15.48 pm

When you create a new project you will see something like the screen shown below. The main parts of the screen are the Hierarchy (which lists all the objects in the current game scene), the Scene window (where you can design the game world and place objects in your game), the Inspector panel (where you can edit the properties of objects in the current scene), and the Project panel (where you can add assets to use in your game project).

Screen Shot 2017-04-14 at 2.16.11 pm

Watch the video below for instructions on how to download and install Unity and begin creating your first 2D game project in Unity.

Next tutorial: Adding assets to your 2D game