Created
March 8, 2013 23:31
-
-
Save SteveSwink/5121007 to your computer and use it in GitHub Desktop.
super simple statemachine based player example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
// An example of how to embed a StateMachine in a Character | |
// and load a few locally defined state methods into it. | |
public class Player : MonoBehaviour | |
{ | |
public float walkSpeed; | |
public float jumpBurst; | |
public float gravity; | |
// public enum PlayerState{ | |
// Idle = 1, | |
// Walk = 2, | |
// WallKick= 3 | |
// } | |
// | |
// PlayerState player = PlayerState.Idle; | |
StateMachine stateMachine = new StateMachine (); | |
// Unity callbacks for setting up an initial state and pumping the | |
// StateMachine on every render cycle | |
void Start () | |
{ | |
// stateMachine.AddState(Player.Idle, enterIDLE, updateIDLE, exitIDLE); | |
stateMachine.ChangeState (enterIDLE, updateIDLE, exitIDLE); | |
} | |
// Create states | |
// All states conform to the StateDelegate method signature | |
// (no input parameters, and a bool return value) | |
// State STATE_IDLE = new State(PlayerState.Idle, enterIDLE, updateIDLE, exitIDLE); | |
// State STATE_WALK = new State(PlayerState.Walk, enterWALK, updateWALK, exitWALK); | |
// IDLE STATE | |
bool enterIDLE () | |
{ | |
return true; | |
} | |
bool updateIDLE () | |
{ | |
float x = Input.GetAxisRaw("Horizontal"); | |
if(x != 0) | |
stateMachine.ChangeState(enterWALK, updateWALK, exitWALK); | |
return false; | |
} | |
bool exitIDLE () | |
{ | |
return true; | |
} | |
// WALK STATE | |
bool enterWALK () | |
{ | |
return true; | |
} | |
bool updateWALK () | |
{ | |
float x = Input.GetAxisRaw("Horizontal"); | |
transform.Translate(new Vector3(x*Time.deltaTime, 0,0)); | |
if(x == 0) stateMachine.ChangeState (enterIDLE, updateIDLE, exitIDLE); | |
return false; | |
} | |
bool exitWALK () | |
{ | |
return true; | |
} | |
// Update the statemachine | |
void Update () | |
{ | |
stateMachine.Execute (); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment