How can I start this animation?

So for my character I have a animation controller. I have an animation attached that is named “Wave”. I need this animation to play when the Space Bar is pressed. So here’s the code I have : (FYI This script is attached to the action on the controller).

public class Wave : StateMachineBehaviour 
{
	public bool x = false;
	public void Update()
	{
		if (x == true) 
		{
			Animation.Play ("Wave");
		}
		if (Input.GetKey (KeyCode.Space)) 
		{
			x = true;
		}
	}
}

What's 'Animation' in the line Animation.Play ("Wave"); ?

Probably something wrong with your start function mate :P

1 Answer

1

You have to use GetComponent<> for referencing a component. And also, you aren’t changing your bool to false anywhere, which will give unexpected results. So maybe try changing that line to (I’m assuming the Animator component is attached to the same gameObject on which this script is attached ) this :

if(x == true){
GetComponent<Animator>().Play("Wave");
x = false;
}