Source

Twisted Potions Double Trouble / Assets / scripts / PlayerChar.cs

Full commit
using UnityEngine;
using System.Collections;

public class PlayerChar : MonoBehaviour {

    private IRagePixel ragePixel;
     
    //enum for character state
    public enum WalkingState {Standing=0, WalkRight, WalkLeft};
    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(int direction) {
		if (direction == (int)WalkingState.WalkRight) {
            state = WalkingState.WalkRight;
		} else if (direction == (int)WalkingState.WalkLeft) {
			state = WalkingState.WalkLeft;									
		} else {
            state = WalkingState.Standing;			
		}		
	}
	
    void Update () {

        Vector3 moveDirection = new Vector3();
        
        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.
				this.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.
				this.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;
        }
        //Move the sprite into moveDirection at walkingSpeed pixels/sec
        transform.Translate(moveDirection * Time.deltaTime * walkingSpeed);
    }
}