- Home /
Having trouble coding an on/off toggle for an animation in script. Please help!
So I have this animation I want to run, and the animation is triggered during an "OnTriggerStay". Within that same OnTriggerStay I want to press a single key to activate the animation and then press the same key to turn it off. I tried to do that but it didnt work because it gets called on multiple times between "on" and "off" so it results in NO CHANGE. Im a newbie so help would be MUCH appreciated. Here is a snippet of the code, I used a bool to try and make it so when i pressed "A" the bool would work as a toggle.
void OnTriggerStay(Collider col)
{
if (Input.GetKeyDown (KeyCode.A) && offon == true)
{
player.GetComponent<Animator> ().SetBool ("doingAnimation",false);
offon = false;
}
if (Input.GetKeyDown (KeyCode.A) && offon == false)
{
player.GetComponent<Animator> ().SetBool ("doingAnimation",true);
offon = true;
}
}
Like I said before, with every 1 press, the animation gets set to true and false multiple times and it results in a net change of nothing. I want to press the "A" key to turn the animation OFF and then press it again to turn it ON. Please help and thank you!
Answer by tahir_ali · Feb 06, 2018 at 12:06 PM
Hey @Newbie100, I hope this helps you!
void OnTriggerStay(Collider col)
{
if(keycheck && Input.GetKeyUp(KeyCode.A))
{
keycheck = false;
}
else if(!keycheck)
{
if (Input.GetKeyDown(KeyCode.A) && offon == true)
{
player.GetComponent<Animator>().SetBool("doingAnimation", false);
offon = false;
keycheck = true;
}
if (Input.GetKeyDown(KeyCode.A) && offon == false)
{
player.GetComponent<Animator>().SetBool("doingAnimation", true);
offon = true;
keycheck = true;
}
}
}
@tahir_ali Thanks for the answer but unfortunately it did not work. For each time i press "A" both of the if statements play. I even put debug.log under both of them to check and yup both play with 1 button press of "A". SetBool("doingAnimation" gets set to false and then right away to true at the same time.
okay so upone further testing I found TWO interesting things. 1: a simple modification to the script made it work. I simply added an "else if" to the second if statement and it succesfully made it work.
void OnTriggerStay(Collider col)
{
if(keycheck && Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.A))
{
keycheck = false;
}
else if(!keycheck)
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A) && offon == true)
{
player.GetComponent<Animator>().SetBool("doingAnimation", false);
offon = false;
keycheck = true;
}
//the change is made here (added else if ins$$anonymous$$d of if)
else if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.A) && offon == false)
{
player.GetComponent<Animator>().SetBool("doingAnimation", true);
offon = true;
keycheck = true;
}
}
}
and the second interesting thing is 2: while using a debug.log under a void Update to check what the status of "keycheck" was i discovered that upon pressing the "A" key it is simultaneously on and off at the same time. However, that does not stop the function of the toggle to properly work. So even though I am skeptical at these findings, it seems to be working so im not going to fiddle with it anymore. Thanks!
Your answer
Follow this Question
Related Questions
How do I make a photography function? 0 Answers
Quiz Game : Valid Question keep decreasing each time the game start. 2 Answers
How do I make my projectile deal damage to a target it hit. 1 Answer
how to make a detect rotation script for negative rotation too 2 Answers
Animations interupting Aim-Script 0 Answers