- Home /
How to check that only one key is pressed
I'm trying to make my animations play when a key is pressed which I have done. But when I press down two buttons that are attached to animations it switches between both animations making it look glitchy. How do I make it so that the animation plays when only the singular key attached it is pressed.
Could you post a bit of your code? I mean, from what you said, the only thing i can think of doing is something like this:
if ( Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.Alpha1 ) ) {
animator.Play( "Animation 1" );
}
else if ( Input.Get$$anonymous$$eyDown( $$anonymous$$eyCode.Alpha2 ) ) {
animator.Play( "Animation 2" );
}
Answer by NoDumbQuestion · Dec 11, 2017 at 08:31 AM
if (Input.anyKeyDown)
{
if (!Input.myKey)
print("Someone type the wrong key");
else if (Input.myKey)
print("You type the key I want");
}
myKey is any button you want to use. For your case, you could deny other input with a simple bool.
public bool isPlaying = false;
private void Update()
{
if (!animationGameObject.isPlaying)
isPlaying = false;
if (Input.anyKeyDown && !isPlaying)
{
if (Input.myKey)
{
isPlaying = true;
print("Do something like animation");
}
}
else
return;
}
Answer by bhavinbhai2707 · Dec 11, 2017 at 08:53 AM
use if else statement with Input.Get()
void Update()
{
if (Input.GetKeyUp (KeyCode.A))
{
Debug.Log ("pressing A");
}
else if (Input.GetKeyUp (KeyCode.S))
{
Debug.Log ("pressing S");
}
}
This should probably give you results of What you want!! and i would suggest using Animator For handling Animations!!
https://www.youtube.com/watch?time_continue=13&v=JeZkctmoBPw
Your answer
![](https://koobas.hobune.stream/wayback/20220612144351im_/https://answers.unity.com/themes/thub/images/avi.jpg)