Animator Scripting

Hey guys. I’ve been trying to learn to make a basic movement script with the animator. I’m new to this, and not sure how it works yet. Hopefully someone can help. In this script, I want certain states in the Animator component to play when the player presses certain buttons. I’m getting the following errors:

  1. Animator Motions.js(30,17): BCE0005: Unknown identifier: ‘animator’.

  2. Animator Motions.js(30,31): BCE0005: Unknown identifier: ‘Walking’.

  3. Animator Motions.js(34,31): BCE0005: Unknown identifier: ‘Running’.

  4. Animator Motions.js(38,31): BCE0005: Unknown identifier: ‘Idle’.

  5. Animator Motions.js(42,31): BCE0005: Unknown identifier: ‘WalkingBackwards’.

  6. Animator Motions.js(46,31): BCE0005: Unknown identifier: ‘LeftStrafe’.

  7. Animator Motions.js(50,31): BCE0005: Unknown identifier: ‘RightStrafe’.

The Script:

var isWalking : boolean;
var isWalkingBackwards : boolean;
var isStrafingRight : boolean;
var isStrafingLeft : boolean;
var isRunning : boolean;
var isIdle : boolean;

// NOTE: The first set of IF statements in Update() are for boolean variables,
// the second is for controlling animations.

function Update() {
	if(Input.GetKeyDown (KeyCode.W))
	{
		isWalking = true;
	}
	if(Input.GetKeyDown (KeyCode.S))
	{
		isWalkingBackwards = true;
	}
	if(Input.GetKeyDown (KeyCode.D))
	{
		isStrafingRight = true;
	}
	if(Input.GetKeyDown (KeyCode.A))
	{
		isStrafingLeft = true;
	}
	if(isWalking == true)		
	{
		animator.Play(Walking);
	}
	if(isRunning == true)		
	{
		Animator.Play(Running);
	}
	if(isIdle == true)		
	{
		Animator.Play(Idle);
	}
	if(isWalkingBackwards == true)		
	{
		Animator.Play(WalkingBackwards);
	}
	if(isStrafingLeft == true)
	{
		Animator.Play(LeftStrafe);
	}
	if(isStrafingRight == true)	
	{
		Animator.Play(RightStrafe);
	}
}

Your animator call is slightly off. You cannot play an animation the way you used to with the Animation.Play(). Instead you need to use the newer syntax.

Example:

Play(“this is the animator state you want to play”, -1, 0);

Explanation:

The string is the name of the animator state you want to play. The -1 is the current layer of the animation (-1 will play the current layer, otherwise you need to keep track of that yourself). The final number is the time at which you want the animation to start.

Hope this helps.

What version of Unity are you using?

In Unity 5 properties like “animator” to get the animator component attached to the same object have been deprecated, you have to get the references using GetComponent: Unity - Scripting API: GameObject.GetComponent

You’re also mixing “animator” and “Animator”. All languages supported by Unity are case sensitive, “animator” and “Animator” are 2 different things (in your case, “animator” is an undefined variable and “Animator” is a class). Pay attention on what you write.

Also, animator.Play takes a string or an int, you’re writing variables that are not defined anywhere. Change things like:

animator.Play(StateName);

to:

animator.Play("StateName");

Anyway, that’s not the best way to use the animator. The animator is a state machine, with states and transitions. You should setup proper parameters and set them to change from one state to another acording to the transitions. Read this link and the next 3 pages: Unity - Manual: Animator Controller