How do I change animation states... or is there a better way?

Hi there, I have been trying to find a way to animate a spritesheet using Unity’s built in animator. So far the best way I have come across is in my code below (Switching animation states using an integer). However the animation is not switching states properly. Example: When I press walk, it walks… but then it will not STOP walking. Can anyone help show me what I am doing wrong here… or if they know of a better way to animate sprites in script the help would be appreciated:D Also, thanks in advanced for any help!

(Please note: I would like to keep variables such as walk speed separate from the animator parameters.)

Complete Character Controls Script:

//Player Controls Script:
//-----------------------

//----------------------
//Public Variables
//----------------------

var walkSpeed					: float = 4.0;		//Walking Speed
var runSpeed					: float = 4.0;		//Runninging speed
var fallSpeed					: float = 2.0;		//Character fall speed
var jumpHeight					: float = 10.0;		//How high the character can jump
var gravity						: float = 20.0;		//Gravity
var startPos					: float = 0.0;

//--------------------
//Is/Enabled Variables
//--------------------

private var crouchEnabled		: boolean = true;
private var jumpEnabled			: boolean = true;
private var idleEnabled			: boolean = true;

private var isCrouching			: boolean = false;
private var isJumping			: boolean = false;
private var isWalking			: boolean = false;

//---------------------
//Storage Variables
//---------------------

var moveDirection				: int = 1;		//Dictates which direction the character faces when the game starts

private var velocity			: Vector3 = Vector3.zero;	//Stores numbers
private var afterHitForceDown	: float = 1.0;				//After it collides, force the player down variable

//---------------------------------
//Player Animation State Variables
//---------------------------------

private var state_Idle		: int = 0;
private var state_Walk		: int = 1;
private var state_Jump		: int = 2;
private var state_Fall		: int = 3;
private var state_Crouch	: int = 4;

//--------------------------
//Animation Variables
//--------------------------
private var currentAnimationState	: int =	state_Idle;
var anim							: Animator;

function Start(){
	anim = GetComponent("Animator");
}

function Update(){

//---------------------------
//Getting components section:
//---------------------------

var aniPlay = GetComponent("Animator");
var controller : CharacterController = GetComponent(CharacterController);	//Gets/Imports the character controller component- NEEDS ONE CONNECTED!
//----------End Of Section---------------

//---------------------------------------------------
//Character movement, crouch, dive, and jump section:
//---------------------------------------------------

if(controller.isGrounded){
	velocity = Vector3 (Input.GetAxis ("Horizontal"), 0, 0);	//Moves the character controller horizontally - CHECK: EDIT/PROJECT SETTINGS/INPUT
	
	//-----------
	//Idle
	//-----------
	
	if(velocity.x == 0 && !isCrouching && idleEnabled){
		isWalking = false;
		ChangeState(state_Idle);
	}
	
	//--------------
	//Movement Left
	//--------------
	
	if(velocity.x < 0){			//Walking animation left
		velocity *= walkSpeed;			//Applies walking speed
		ChangeState(state_Walk);	//Plays Walking animation left
		idleEnabled = false;
		isWalking = true;
	}
	else{
		idleEnabled = true;
		isWalking = false;
	}
	
	//---------------
	//Movement Right
	//---------------
	
	if(velocity.x > 0){			//Walking animation right
		velocity *= walkSpeed;	//Applies walking speed
		ChangeState(state_Walk);//Plays Walking animation right
		idleEnabled = false;
		isWalking = true;
	}
	else{
		idleEnabled = true;
		isWalking = false;
	}
	
	//--------------
	//The Jump Move
	//--------------
	
	if(Input.GetButton ("Jump") && jumpEnabled && !isCrouching){
		isJumping = true;
		crouchEnabled = false;
		velocity.y = jumpHeight;	//This causes the player to jump
		ChangeState(state_Jump);	//Plays jumping up animation
	}
	else if(!isJumping){
		crouchEnabled = true;
		jumpEnabled	= true;
		
		isCrouching = false;
		isJumping = false;
	}
	
}

//-----------------
//Movement in air
//-----------------

if(!controller.isGrounded){
	velocity.x = Input.GetAxis("Horizontal");//This allows the player to move while in air/jumping
	velocity.x *= runSpeed;
}

//---------
//Falling!
//---------

if(!controller.isGrounded && controller.velocity.y < 0){
	ChangeState(state_Fall);//Plays falling down animation
}

//-----------------
//The crouch move
//-----------------

if(velocity.x == 0 && Input.GetAxis("Vertical") < 0 && controller.isGrounded && crouchEnabled && !isJumping){
	isCrouching = true;
	jumpEnabled = false;
	ChangeState(state_Crouch);//Plays crouching animation
}
else if(!isCrouching){
	crouchEnabled = true;
	jumpEnabled	= true;
	
	isCrouching = false;
	isJumping = false;
}


//-----------End Of Section----------------------

//----------------------------------------------
//Stores which direction the player is facing:
//----------------------------------------------

if(velocity.x < 0){	//Character facing left
	moveDirection = 0;
}

if(velocity.x > 0){	//Character facing right
	moveDirection = 1;
}
//---------End Of Storing Direction--------------

//----------------------------
//Controller Collision Center
//----------------------------

//--------------------------------------------------
//Collide with roof (Above), aftger hit force down
//--------------------------------------------------
if(controller.collisionFlags == CollisionFlags.Above){
	velocity.y = 0;
	velocity.y -= afterHitForceDown;	//Applies downward force to player
}
//-----------End of Secction--------------------

//---------------------------
//Necessary Stuff
//---------------------------
velocity.y -= gravity * Time.deltaTime;		//Applies gravity
controller.Move(velocity * Time.deltaTime);	//Moves the controller
}

//-----------------------
//Change State Function
//-----------------------

function ChangeState(State : int){
if (currentAnimationState == State)
        return;
 
        switch (State) {
 
       		case state_Idle:
	            anim.SetInteger ("State", state_Idle);
	            break;
	            
	        case state_Walk:
	            anim.SetInteger ("State", state_Walk);
	            break;
	        
	        case state_Jump:
	            anim.SetInteger ("State", state_Jump);
	            break;
	        
	        case state_Fall:
	        	anim.SetInteger ("State", state_Fall);
	        
	        case state_Crouch:
	            anim.SetInteger ("State", state_Crouch);
	            break;
 
        }
}

Wow, thats complex! Seriously, you are making matters too hard for yourself.

There is a great video http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers which gives an example, and the code is a lot simpler (read: easier to maintain) than yours currently is.

Looking at the code, it looks like you have too many bool variables. I’m guessing (I have not tried to run the code), that with so many variables they are not always being reset properly. If I had to guess, I think its either the idleEnabled or isWalking, because you seem the set them every cycle. If you look at the code you have:

if(velocity.x < 0){...     }
     else{
         idleEnabled = true;
         isWalking = false;
     }
     
     
     if(velocity.x > 0){ ... }
     else{
         idleEnabled = true;
         isWalking = false;
     }

So it VERY likely that these two are going to be set.

To fix this:

  • Set those enabled variables to public (FOR NOW), so you can easily view them in the inspector. I’m guessing that they are not being reset properly (you can probably lose at least half of the variables and skill have it work correctly) OR
  • Watch the video and redesign your script again