how to make an animation play on keypress unity game maker
so what i want to do is when the key is not pressed i want my character to play the "idle" animation, but when I want my character to walk i want him to play the "sprite" animation, I am super new to unity and will appreciate to know what i can do to fix this
public Animator anim;
public KeyCode RightKey;
// Use this for initialization
void Start()
{
if (Input.GetKeyDown(RightKey))
anim.Play("sprite");
if (Input.GetKeyUp(RightKey))
anim.SetTrigger("idle");
}
// Update is called once per frame
void Update()
{
}
}
Answer by Aaranos · Jun 10, 2017 at 09:34 PM
You have to put your if statements in Update. The Start function is only ran at start so it wouldn't check for them later.
If you haven't already, you will also want to setup the animations in Animator. You would want your Idle to be the default. Your trigger would be assigned to the animation you want to trigger, and then it would go back to Idle. That eliminates the need to check for key up.
Also, you will have to assign your animator component.
A few reference pages that may help: https://docs.unity3d.com/ScriptReference/KeyCode.html https://docs.unity3d.com/Manual/class-Animator.html
public Animator anim;
// Use this for initialization
void Start()
{
anim = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
anim.Play("sprite");
}
And can i do an else statement? like when I remove the Down and when I don't press the Button to it to stop
doesn't work, as it shows the following error ![alt text][invalid layer index '-1']
[1]: /storage/temp/194801-error-animate.png
Your answer