Hi fire7side, thank you for the interest. I cut the code due to the extension. Here you have a complete code (the error is at line 142 )
#pragma strict
var direction = 1; // 0 face left, 1 face right
var inputX = .0;
var timeJumpPressed = .0; // time the jump button is pressed
var highJumpThreshold = .3;
var runSpeed = 8.0;
var walkRunRatio = 1.5;
var fsm : FSM;
private var moveDirection = Vector3.zero;
var dir : Transform;
function SetTransition(t : Transition) {
fsm.PerformTransition(t);
}
function Start () {
dir = gameObject.GetComponent(Transform);
MakeFSM();
}
function Update () {
inputX = Input.GetAxis("Horizontal");
if (inputX < 0) {
//direction = 0;
}
if (inputX > 0) {
//direction = 1;
}
if (Input.GetButton("Jump")){
timeJumpPressed += Time.deltaTime;
}
fsm.currentState.Reason(this.gameObject);
fsm.currentState.Action(this.gameObject);
}
// create the fsm
function MakeFSM() {
var one = Idle();
one.AddTransition(Transition.idleWalk, StateId.walk);
one.AddTransition(Transition.idleJump, StateId.jump);
one.AddTransition(Transition.idleHighjump, StateId.highjump);
one.AddTransition(Transition.idleTurnaround, StateId.turnaround);
var two = Walk();
two.AddTransition(Transition.walkIdle, StateId.idle);
two.AddTransition(Transition.walkRun, StateId.run);
var three = Jump();
three.AddTransition(Transition.jumpIdle, StateId.idle);
var four = Highjump();
var five = Run();
five.AddTransition(Transition.runWalk, StateId.walk);
var six = Turnaround();
six.AddTransition(Transition.turnaroundIdle, StateId.idle);
fsm = FSM();
fsm.AddState(one);
fsm.AddState(two);
fsm.AddState(three);
fsm.AddState(four);
fsm.AddState(five);
fsm.AddState(six);
}
class Idle extends FSMState {
function Idle() {
stateId = StateId.idle;
}
// decides if the state should transition to another on its list
function Reason(player : GameObject) {
if (player.GetComponent(example).direction == 1 Input.GetButtonDown("Jump") ) {
player.GetComponent(example).SetTransition(Transition.idleTurnaround);
}
}
function Action(player : GameObject) {
player.GetComponent(example).aniSprite(16, 6, 0, 0, 16, 16);
}
}
class Walk extends FSMState {
function Walk() {
stateId = StateId.walk;
}
function Reason(player : GameObject) {
}
}
function Action(player : GameObject) {
player.GetComponent(example).aniSprite(16, 6, 0, 1, 16, 16);
}
}
class Jump extends FSMState {
function Jump() {
stateId = StateId.jump;
}
function Reason(player : GameObject) {
}
function Action(player : GameObject) {
}
class Highjump extends FSMState {
function Highjump() {
stateId = StateId.highjump;
}
function Reason(player : GameObject) {
}
function Action(player : GameObject) {
player.GetComponent(example).aniSprite(16, 6, 0, 4, 16, 16);
}
}
class Run extends FSMState {
function Run() {
stateId = StateId.run;
}
function Reason(player : GameObject) {
}
function Action(player : GameObject) {
player.GetComponent(example).aniSprite(16, 6, 0, 2, 16, 16);
}
}
class Turnaround extends FSMState {
function Turnaround() {
stateId = StateId.turnaround;
}
function Reason(player : GameObject) {
yield player.GetComponent(example).StartCoroutine(player.GetComponent(example).wait(1));
player.GetComponent(example).SetTransition(Transition.turnaroundIdle);
}
function Action(player : GameObject) {
player.GetComponent(example).aniSprite(16, 6, 0, 5, 5, 16);
}
}
function wait(waitTime : float) {
yield WaitForSeconds(waitTime);
}
function aniSprite(columnSize : float, rowSize : float, colFrameStart : float, rowFrameStart : float, totalFrames : float, framesPerSecond : float) {
var index : int = Time.time * framesPerSecond; // time control fps
index = index % totalFrames; // modulate
var size = Vector2(1.0 / columnSize, 1.0 / rowSize); // scale
var u : int = index % columnSize;
var v : int = index / columnSize;
var offset = Vector2((u + colFrameStart) * size.x, (1 - size.y - (v + rowFrameStart) * size.y)); // offset
renderer.material.mainTextureOffset = offset; // texture offset
renderer.material.mainTextureScale = size; // texture scale
//renderer.material.SetTextureOffset("_BumpMap", offset);
//renderer.material.SetTextureScale("_BumpMap", size);
}
I think it is something related with child classes and yield but it would be great if someone a little more technical could explain what is going on…
Thanks in advance