Tag Archives: animation

Controlling 2D player animations with C# code in Unity

WARNING: If you found this page linked from a paid course on Udemy, you have been ripped off. This course is available as a free video tutorial series here and on my YouTube channel. If you have paid for these tutorials, contact Udemy for a refund and report the course to Udemy. I created this course with the intention of it being available to anyone for free.

In this tutorial, you will learn how to control player animations in your 2D game with C# code in Unity. Watch the video below and then scroll down for 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;
  private Animator playerAnimation;

  // Use this for initialization
  void Start () {
    rigidBody = GetComponent<Rigidbody2D> ();
    playerAnimation = GetComponent<Animator> ();
  }
  
  // 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);
    }

    playerAnimation.SetFloat ("Speed", Mathf.Abs (rigidBody.velocity.x));
    playerAnimation.SetBool ("OnGround", isTouchingGround);
  }
}

Next tutorial:  Flipping the player with code

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

Create a looping walk cycle animation in Adobe Flash / Animate

This tutorial explains how to create a looping walk cycle animation in Adobe Flash (Adobe Animate) like the one shown below.

WalkCycle

You can use the same technique for other types of internal animated loops. Watch the video below to find out how to do this using either the Adobe Flash or Adobe Animate software.

In this video I used the sprite sheet by soldiern at http://soldiern.deviantart.com/art/Sub-Zero-Sprite-Sheet-V2-210769129 (using Creative Commons CC3.0 license – https://creativecommons.org/licenses/by-sa/3.0/ ).

Storyboarding tips and template

A storyboard is a graphic organiser that is made up of illustrations or images displayed in sequence and is used to pre-visualise a movie, animation, or interactive media sequence. Storyboards are also used to plan out the scenes in video games and screens in software applications.

Storyboards look a little like comics. They show movement, position of objects, camera positions and angles, lighting, and usually include labels explaining the scene and the animation or filming techniques to be used. Storyboards can be in the form of very basic sketches or can be very detailed like the example shown below.

Source: http://movillefacepalmplz.deviantart.com/art/something-fishie-storyboard-345334144 . Used under Creative Commons CC3.0 license: https://creativecommons.org/licenses/by-sa/3.0/
Source: http://movillefacepalmplz.deviantart.com/art/something-fishie-storyboard-345334144 . Used under Creative Commons CC3.0 license: https://creativecommons.org/licenses/by-sa/3.0/

The storyboarding process, in the form that is used today, was developed at Walt Disney Productions in the early 1930s. Today, there are many programs and tools that can help to make storyboarding easy, however many people still choose to sketch a storyboard using pencil and paper.

Storyboard template

Click here to download a printable storyboard template (or click on the thumbnail image below). This storyboard template has 12 frames but you can easily add more frames to it.

storyboard_template

Storyboarding apps and tools

  • Storyboardthat.com is a web app that you can use to draw a storyboard using existing backgrounds, objects and characters and a range of tools to add text, arrows, etc.

Storyboarding tips

Storyboarding tips can be accessed from the links below.

Connecting joints in Maya

This video tutorial explains how to connect joints in Maya when working with skeletons, and also how to add bones between joints. The video also gives an introduction to the parent-child relationship in Maya. If you haven’t already watched the video on creating skeletons, then click here to watch it first.

After connecting one joint to another joint in Maya, you will notice the parent-child relationship (hierarchy) update when viewing the list of different joints in the Outliner (click on Windows > Outliner).

Joint parent-child relationship in Outliner view.
Joint parent-child relationship in Outliner view.

Animate a skeleton on a motion path in Maya

This video explains how to animate an object’s skeleton to a motion path in Maya. When you animate a object with its skeleton attached to a motion path, the object can bend along the curve. This is useful if you are trying to animate the movement of something like a fish or a snake.

An object with its skeleton attached to a motion path.
An object with its skeleton attached to a motion path.

Watch the video to find out how to animate a skeleton to a motion path.

Note: It is important to follow the step where you rotate the object so that it is facing straight towards you when viewing from the right-side view. If you don’t do this then the motion path animation will be a bit buggy. It is also important to remember to click on Modify and Centre Pivot before attaching the object to the motion path.

Creating and animating 3D text in Maya

This video explains how to add text to a scene and then animate it in Maya. You can manipulate text in the same way you manipulate many other 3D objects in Maya – you can move (translate), rotate, scale, set keyframes or even animate along a path.

Watch the video below to find out how to create and animate 3D text in Maya.

Extruding along a curve

This video explains how to extrude a circle along a curve and then animate an extrusion. When we ‘extrude’ a 2D shape such as a circle, it means that we will push that shape out. By using a curved line, we can push the shape out so that it follows the line.

An extruded circle along a curve
An extruded circle along a curve

Watch the video below to see how to do this in Maya.

Tip: When selecting the circle and CV curve, select the circle first and then hold down Shift key to select the CV curve. Make sure you select the curve last.

Keyframe animation in Maya

This tutorial video explains how to animate a shape in Maya using keyframe animation on the timeline.

Tip: To change the frame rate (frames per second) of your animation go to Maya > Preferences > Settings. Under ‘Working units‘, you can change the frame rate in the drop-down box next to Time. The default frame rate is Film (24fps).