Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Newbie100 · Feb 06, 2018 at 11:53 AM · animationscripting problemscripting beginnerscriptingbasicstoggle

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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;
         }
     }

     
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Newbie100 · Feb 07, 2018 at 07:14 AM 0
Share

@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.

avatar image Newbie100 · Feb 07, 2018 at 07:25 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

251 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges