Runtime Animated Puppet (Character Controller Tutorial)

In this video we go from creating a character using self made assets to animating it and controlling the animations and movement via the keyboard! There’s a lot to take in, but I walk you through every step.

We’re going to have a lot of fun.

You are welcome to use the sprite sheet I created in the video if you are having trouble with making one of your own. You can use it in any project you like, but please credit my Youtube video!

Blink.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Blink : MonoBehaviour
{

	public Sprite eyeWhites;
	public Sprite eyeBlinking;
	public GameObject pupils;

	private float timer;
	private float blinkspeed;
	private SpriteRenderer sRenderer;

	void Start ()
	{

		timer = 0;
		RandomiseBlinkSpeed ();
		sRenderer = GetComponent<SpriteRenderer> ();

	}

	void Update ()
	{

		if (!PuppetMovement.happyFace) {
			timer += Time.deltaTime;
		}

		if (timer >= blinkspeed) {
			timer = 0;
			RandomiseBlinkSpeed ();
			sRenderer.sprite = eyeBlinking;
			sRenderer.color = skinColor.skin;
			pupils.GetComponent<SpriteRenderer> ().enabled = false;
			StartCoroutine (BlinkEyes ());
		}

	}

	private void RandomiseBlinkSpeed ()
	{
		blinkspeed = Random.Range (2, 6);

	}

	IEnumerator BlinkEyes ()
	{
		yield return new WaitForSeconds (0.2f);
		sRenderer.sprite = eyeWhites;
		pupils.GetComponent<SpriteRenderer> ().enabled = true;
		sRenderer.color = Color.white;
	}

}
SkinColor.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SkinColor : MonoBehaviour
{

	public static Color skin;
	public SpriteRenderer myRenderer;

	// Use this for initialization
	void Start ()
	{
		skin = myRenderer.color;
		
	}
	
	// Update is called once per frame
	void Update ()
	{
		
	}
}


PuppetMovement.cs

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PuppetMovement : MonoBehaviour
{

	public Animator bodyAnim;
	public Animator lArmAnim;
	public Animator rArmAnim;
	public static bool larmup = false;
	public static bool rarmup = false;

	public GameObject puppetContainer;

	public Sprite[] mouthClosedShapes;
	public Sprite[] mouthOpenShapes;
	public SpriteRenderer mouthRenderer;
	private bool mouthOpen;


	private bool doOnce = false;

	public static bool happyFace = false;
	public SpriteRenderer eyeRenderer;
	public SpriteRenderer pupilsRenderer;
	public Sprite eyesOpen;
	public Sprite eyesClosed;
	public Sprite pupils;

	// Use this for initialization
	void Start ()
	{

	}

	// Update is called once per frame
	void Update ()
	{

		if (Input.GetKey (KeyCode.Q)) {
			lArmAnim.SetBool ("armUp", true);
			larmup = true;
		} else {
			lArmAnim.SetBool ("armUp", false);
			larmup = false;
		}

		if (Input.GetKey (KeyCode.E)) {
			rArmAnim.SetBool ("armUp", true);
			rarmup = true;
		} else {
			rArmAnim.SetBool ("armUp", false);
			rarmup = false;
		}



		if (Input.GetKey (KeyCode.S)) {
			bodyAnim.SetBool ("walking", true);
			pupilsRenderer.sprite = null;
			eyeRenderer.sprite = eyesClosed;
			mouthRenderer.sprite = mouthOpenShapes [0];
			happyFace = true;
		}

		if (Input.GetKeyUp (KeyCode.S)) {
			bodyAnim.SetBool ("walking", false);
			pupilsRenderer.sprite = pupils;
			eyeRenderer.sprite = eyesOpen;
			mouthRenderer.sprite = mouthClosedShapes [0];
			happyFace = false;
		}

		if (Input.GetKeyDown (KeyCode.A)) {
			puppetContainer.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (-200, 0));
		}
		if (Input.GetKey (KeyCode.A)) {
			bodyAnim.SetBool ("walking", true);
		}
		if (Input.GetKeyUp (KeyCode.A)) {
			bodyAnim.SetBool ("walking", false);
			puppetContainer.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (200, 0));
		}

		if (Input.GetKeyDown (KeyCode.D)) {
			puppetContainer.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (200, 0));
		}
		if (Input.GetKey (KeyCode.D)) {
			bodyAnim.SetBool ("walking", true);
		}
		if (Input.GetKeyUp (KeyCode.D)) {
			bodyAnim.SetBool ("walking", false);
			puppetContainer.GetComponent<Rigidbody2D> ().AddForce (new Vector2 (-200, 0));
		}

		if (Input.GetKey (KeyCode.W)) {
			
			if (doOnce == false) {
				doOnce = true;
				int a = Random.Range (0, mouthOpenShapes.Length);
				int b = Random.Range (0, mouthClosedShapes.Length);
				if (mouthOpen) {
					mouthRenderer.sprite = mouthClosedShapes [b];
					mouthOpen = false;
				} else {
					mouthRenderer.sprite = mouthOpenShapes [a];
					mouthOpen = true;
				}
				StartCoroutine (Talking ());
			} 
		}

		if (Input.GetKeyUp (KeyCode.W)) {
			mouthRenderer.sprite = mouthClosedShapes [0];
			mouthOpen = false;
		}
	}
		
	IEnumerator Talking ()
	{
		yield return new WaitForSeconds (0.2f);
		doOnce = false;
	}
}

 

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *