Wednesday, September 24, 2014

Character Player Controller

enum GameState
{
idle,
runLeft,
runRight,
jumpLeft,
jumpRight,
idleLeftJump,
idleRightJump,
celebrate,
die
}

var gameState:GameState;
private var moveDirection=true;
private var exit:boolean=false;
private var youWin:boolean=false;
private var youDie:boolean=false;
function Update(){
//Define GameState
if(Input.getAxis("Horizontal")>0){
moveDirection=true;
if(Input.GetButton("Jump")){
gameState=GameState.jumpRight;
}else{
gameState=GameState.runRight;
}
}else if(Input.getAxis("Horizontal")<0){
moveDirection=false;
if(Input.GetButton("Jump")){
gameState=GameState.jumpLeft;
}else{
gameState=GameState.runLeft;
}
}else{
if(Input.GetButton("Jump")){
if(moveDirection){
gameState=GameState.idleRightJump;
}else{
gameState=GameState.idleLeftJump;
}
}else{
gameState=GameState.idle;
}
}
if(youWin){
gameState=GameState.celebrate;
}
if(youDie){
gameState=GameState.die();
}
//Assign every state with a Vector3(x,y,x) variable
if(controller.isGrounded){
switch(gameState){
case GameState.runRight:
velocity=speed*Vector3(1,0,0);
break;
case GameState.runLeft:
velocity=speed*Vector3(-1,0,0);
break;
case GameState.jumpRight:
velocity=speed*Vector3(1,jumpSpeed,0);
break;
case GameState.jumpLeft:
velocity=speed*Vector3(-1,jumpSpeed,0);
break;
case GameState.idleLeftJump:
case GameState.idleRightJump:
velocity=speed*Vector3(0,jumpSpeed,0);
default:
velocity=Vector3.zero;
break;
}
}else{
velocity.y-=gravity*Time.deltaTime;
}
//Implement movement
controller.Move(velocity*Time.deltaTime);
}

No comments:

Post a Comment