- Home /
Play the same animation with 3 different buttons?
Hello, maybe i'm missing something simple here, but I cant seem to get my "firing" animation to play with 3 different button presses. I have it working so that when the spacebar is pressed, the animation plays and it works, but if I try to code in other buttons like leftshit, it doesnt work. Here's the code:
public class animfire2 : MonoBehaviour {
Animator m_Animator;
bool Fire;
void Start () {
m_Animator = gameObject.GetComponent<Animator>();
Fire = false;
}
void Update () {
if (Input.GetKey (KeyCode.LeftShift))
Fire = true;
else
Fire = false;
if (Fire == false)
m_Animator.SetBool ("Fire", false);
if (Fire == true)
m_Animator.SetBool ("Fire", true);
}
}
Answer by JimmyCushnie · Feb 17, 2018 at 06:32 PM
This is how to detect multiple key codes:
if (Input.GetKey (KeyCode.LeftShift) || Input.GetKey(KeyCode.Space))
|| means "or", so now the code under the if will run if either or both leftshift or space are pressed. You should really use GetAxis instead of GetKey, though, because then players can rebind they keys to whatever they want. You can also just set an axis to respond to multiple keys, so you don't even need to do multiple checks in the if statement.
Glad to hear it :) best of luck with your project.