Source

Twisted Potions Double Trouble / Assets / Scripts / PlayerCharacter.cs

using UnityEngine;
using System.Collections;

public class PlayerCharacter : MonoBehaviour
{

	private IRagePixel ragePixel;
     
	//enum for character state
	public enum WalkingState
	{
		Standing=0,
		WalkRight,
		WalkLeft,
		Jump,
		Casting
	};
	public WalkingState state = WalkingState.Standing;
	public bool flipState = false;
	
	//walking speed (pixels per second)
	public float walkingSpeed = 10f;

	void Start ()
	{
		ragePixel = GetComponent<RagePixelSprite> ();
	}

	public void Move (WalkingState direction)
	{
		if (direction == WalkingState.WalkRight) {
			state = WalkingState.WalkRight;
		} else if (direction == WalkingState.WalkLeft) {
			state = WalkingState.WalkLeft;									
		} else if (direction == WalkingState.Jump) {
			state = WalkingState.Jump;		
		} else if (direction == WalkingState.Casting) {
			state = WalkingState.Casting;				
		} else {
			state = WalkingState.Standing;		
		}
	}
	
	void Update ()
	{
		Vector3 moveDirection = Vector3.zero;
		switch (state) {
		case(WalkingState.Standing):
                //Reset the horizontal flip for clarity
			ragePixel.SetHorizontalFlip (flipState);
			ragePixel.PlayNamedAnimation ("Stand", false);
			break;

		case (WalkingState.WalkLeft):
				//Not flipping horizontally. Our animation is drawn to walk right.
			flipState = false;
			ragePixel.SetHorizontalFlip (flipState);
                //PlayAnimation with forceRestart=false. If the WALK animation is already running, doesn't do anything. Otherwise restarts.
			ragePixel.PlayNamedAnimation ("Walk", false);
                //Move direction. X grows right so left is -1.
			moveDirection = new Vector3 (-1f, 0f, 0f);
			break;

		case (WalkingState.WalkRight):
                //Flip horizontally. Our animation is drawn to walk left.
			flipState = true;
			ragePixel.SetHorizontalFlip (flipState);
                //PlayAnimation with forceRestart=false. If the WALK animation is already running, doesn't do anything. Otherwise restarts.
			ragePixel.PlayNamedAnimation ("Walk", false);
                //Move direction. X grows right so left is +1.
			moveDirection = new Vector3 (1f, 0f, 0f);
			break;

		case (WalkingState.Jump):
			ragePixel.SetHorizontalFlip (flipState);
                //PlayAnimation with forceRestart=false. If the WALK animation is already running, doesn't do anything. Otherwise restarts.
			ragePixel.PlayNamedAnimation ("Jump", false);
			break;
		
		case (WalkingState.Casting):
			ragePixel.SetHorizontalFlip (flipState);
			//PlayAnimation with forceRestart=false. If the WALK animation is already running, doesn't do anything. Otherwise restarts.
			ragePixel.PlayNamedAnimation ("Cast", false);
			break;
		
		}
		//Move the sprite into moveDirection at walkingSpeed pixels/sec
		transform.Translate (moveDirection * Time.deltaTime * walkingSpeed);
	}
}