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 Bryangs · Jun 12, 2017 at 12:59 AM · scripting problemanimatorchangebooldynamically

Best way of using animator bools in a script

Hey, sorry if the title is strange, i kinda don't know exactly what to type in as a title. Anyway, i am intrigued. I am working on a mobile game, and i am doing my best to keep everything as smooth and mobile friendly as possible, but i am facing a problem with a script of mine.

I have two "tilt" animation for my character for when the player is rotating the device, and i want to trigger them accordingly. For now i am testing only on the PC, and i really want answers for PC too bcs i know how to translate them to mobile later. Here is an example of what i am using:

 void Update()
 {
        If (Input.GetKey("a")
         {
              anim.SetBool("TiltLeft",true);
         }
 
        If (!Input.GetKey("a")
         {
              anim.SetBool("TiltLeft",false);
         }
 }

But to be sincere, this method is horrible for performance, as it is placed on Update. I was thinking about adding a void LateUpdate and place the code there, but this would mean to have two... "fast" voids at the same time. I thought about using a bool, but this would also be insignificant. My question is: What would be the best method to implement this "dynamic" bool system into my script?

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

2 Replies

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

Answer by hassanyawar · Jun 12, 2017 at 08:10 AM

As a word of advice, first thing you need to do is make hashes of your animation strings in Awake () or Start () maybe. Hash is basically int and processing int is faster than processing string as it doesn't create garbage.

To do that create a variable, like:

 private int tiltLeftBoolHash;
 
 void Awake ()
 {
        tiltLeftBoolHash = Animator.StringToHash("TiltLeft");
 }

If you check the overloaded method of SetBool, you will notice that it also takes input parameter of int instead of string.

You can also look it up at these links: https://docs.unity3d.com/ScriptReference/Animator.StringToHash.html https://unity3d.com/learn/tutorials/topics/animation/animator-scripting

Now coming on to the main point, you can do something like this:

 if (Input.GetKeyDown(KeyCode.A))
 {
        anim.SetBool(tiltLeftBoolHash, true);
 }
 
 if (Input.GetKeyUp(KeyCode.A))
 {
        anim.SetBool(tiltLeftBoolHash, false);
 }

Instead of checking in if whether the a is being currently pressed or not pressed, I think the more appropriate way is to use Down and Up variations of GetKey. GetKeyUp is only called in Update in that frame when you pressed the a key. Its not called after that. GetKeyDown is only called in Update in that frame when you released the a key.

Do let me know if it helps.

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 Bryangs · Jun 12, 2017 at 09:45 PM 1
Share

Smart idea, mate. Thanks for the help!

avatar image hassanyawar Bryangs · Jun 13, 2017 at 07:30 AM 0
Share

I'm glad I could be of help!

avatar image
0

Answer by kornstar83 · Jun 12, 2017 at 02:47 AM

if you also checked for the value of the animator bool in your if statements you significantly reduce the amount of times that the SetBool function is called. I'm not sure that this is the most efficient way but it should help .

  void Update()
  {
         If (Input.GetKey("a") && !anim.GetBool("TiltLeft")
          {
               anim.SetBool("TiltLeft",true);
          }
  
         If (!Input.GetKey("a") && anim.GetBool("TiltLeft")
          {
               anim.SetBool("TiltLeft",false);
          }
  }
Comment
Add comment · 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

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

109 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

Related Questions

Animator parameter stays true after mouse button released. 1 Answer

Destroying gameobject after animation 1 Answer

2d Character runs in place via animator 0 Answers

Why is my Character staying in Air Idle rather than Grounded Idle? 0 Answers

How to dynamically change a movie texture from Streaming Assets for a windows build 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