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 /
  • Help Room /
avatar image
0
Question by Chris100100 · Oct 24, 2015 at 09:24 PM · animationtriggerbooleankeypress

Selecting animation to play based on boolean

I'm trying to make a certain animation play when the player hits a trigger and presses a key. I have a boolean which gets changed depending on the animation that should be played. The problem is that it only ever plays one animation and I'm not sure why.

Could anyone help me find out the problem?

Updated code

 var animObj : GameObject; //The game object I want to be animated
 
 var nameAnim : String; //The name of the first animation
 var nameAnim2 : String; //THe name of the "reverting" animation
 
 var tagName : String; //If the animated object touches a trigger with this name, play an animation
 var triggered = false;//Decides which animation is appropriate to play
 var onTrig = false;
 
 
 function OnTriggerEnter (other : Collider) //{
 
 {
     if (other.gameObject.tag == tagName) //The player has touched the animated object
     {
         onTrig = true;
     }
 }
 
 function OnTriggerExit(other : Collider)
 {
     onTrig = false;
 }
 
 function Update()
 {
 
     if (onTrig == true)
     {
         if (Input.GetKeyDown ("e"))//If the player presses this key
         { 
             if (!triggered)
             {
                 animObj.GetComponent.<Animation>().Play(nameAnim); //play the animation
                 triggered = true;
             }
     
             else if (triggered)
             {
                 animObj.GetComponent.<Animation>().Play(nameAnim2); //Play the animation that brings the animated object back to the start
                 triggered = false;
             }            
         }
     }
 }


Comment
Add comment · Show 1
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 Statement · Oct 24, 2015 at 09:36 PM 0
Share

Do you get any errors in the console? Do you get the messages "I am playing the first animation" and "I am playing the second animation"?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Statement · Oct 24, 2015 at 09:35 PM

Ok, that's a little strange setup. So OnTriggerStay can be called multiple times per frame. So expect that function to run a lot. Then, if you happen to hold down e, you'll play an animation based on the bool, and invert the bool.

triggered will be switching true,false,true,false,true,false,true,false and so on many many times per second, and you'll be starting the animation many times per second.

One suggestion would be to stop using GetKey, which will return true every frame you hold down the key and instead use GetKeyDown which will return true that frame you actually started holding down e. However this is likely still going to cause problems because OnTriggerStay can be called multiple times per frame, so your bool will still wiggle its value like a happy dogs tail, just in small bursts. If you run dry if project name suggestions; "Tail burst" could give you a fond memory of this dilemma. Anyway.

I'd move the Input and Animation code to Update instead of FixedUpdate for those reasons. Then I'd also no longer use OnTriggerStay but instead use OnTriggerEnter and OnTriggerExit to set a bool wether or not you are in a trigger or not.

Comment
Add comment · Show 7 · 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 Chris100100 · Oct 25, 2015 at 01:52 AM 0
Share

Thank you, the code is already working much better than before! I put the triggered boolean changes in Update and replaced Get$$anonymous$$ey with Get$$anonymous$$eyDown. However, the wrong animation will still sometimes play and putting OnTriggerEnter ins$$anonymous$$d of OnTriggerStay makes the code not work at all and I'm not sure why.

avatar image Statement Chris100100 · Oct 25, 2015 at 09:58 AM 0
Share

Edit your original question to update the code example there, so we can see..

avatar image Chris100100 Statement · Oct 25, 2015 at 05:57 PM 0
Share

Alright I updated the original code

avatar image Statement Chris100100 · Oct 25, 2015 at 06:55 PM 0
Share

Both your input and animation code is still in FixedUpdate. Read what I wrote and try again.

avatar image Chris100100 Statement · Oct 25, 2015 at 07:20 PM 0
Share

I didn't know that OnTriggerEnter was a FixedUpdate, my bad! I think these changes are what you wanted?

Show more comments
Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make my Power Button stop my Conveyor Belt 0 Answers

Play fbx animation on trigger and keypress 0 Answers

ANIMATION STRANGELY DOES NOT PLAY 0 Answers

Timer onTriggerEnter doesn't launch 0 Answers

Animation triggered by other animation, script, c# 1 Answer


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