- Home /
If your pressing one key, how to disable another?
Hi, for my game I need a simple function that says if "Sprint" is pressed "Fire1" and "Fire2" cannot be pressed. I've just started using unity and i have tried everything I know to try and get this to work. I'm sure this is very easy for someone, so it would be great if anyone could give me a pointer of how to do it. (And possibly explain how it works because, I don't just want to copy and paste.)
Hope someone can help, Thanks. Andy123W
Answer by bompi88 · Apr 28, 2012 at 02:05 PM
You can try something like this:
if (Input.GetButton("Sprint"))
{
print ("Sprinting");
} else {
if (Input.GetButton("Fire1"))
{
print ("Fire1 action");
}
if (Input.GetButton("Fire2"))
{
print ("Fire2 action");
}
}
Answer by Andy123W · Apr 28, 2012 at 04:29 PM
Just in case anyone else wants to know, I found a solution where just before the shooting code (Fire1) and the ADS code (Fire2) I put the code:
if(Input.GetButton("Sprint") == false)
This means that you can only run the "shooting" and "Aim down sight", code if you are not holding down the "Sprint" button.