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 Trevdevs · Aug 01, 2016 at 06:28 PM · animatorhash

Animator SetBool not changing to true C#

I'm quite positive I have done everything correctly however the Animator still doesn't want to set the parameters to true so the transition can take effect. If you would like me to post a photo of the animator I will however I assure you the parameters are setup correctly with the transitions and all spelled correctly.

Here I assign the hash and bool values. public KeyCode Forward; public KeyCode Backward; public KeyCode Left; public KeyCode Right;

 public Animator PlayerAnimations;
 public bool IsMoving;
 public int moveHash;
 public int attackHash;
 public bool CanAttack;
 
 void Start()
 {
     Forward = KeyCode.W
     Backward = KeyCode.S
     Left = KeyCode.A
     Right = KeyCode.D
     
     PlayerAnimations = GetComponent<Animator>();
 
     moveHash = Animator.StringToHash("IsMoving");
     attackHash = Animator.StringToHash("IsAttacking");
     IsMoving = true;
     CanAttack = true;
 }

Then here I tell it if one of the 4 movement keys are hit switch the parameter for the moving transition to true, and likewise for the attack if the left mouse button is clicked

     void Update()
     {
         //If one of the 4 movement keys are hit transition into moving animation or if not go back to idle
         if (Input.GetKey(Forward))
             
         {
             transform.Translate(Vector3.forward * 5 * Time.deltaTime);
             IsMoving = true;
             PlayerAnimations.SetBool(moveHash, IsMoving);
         }
         if(Input.GetKey(Backward))
         {
             transform.Translate(Vector3.back * 5 * Time.deltaTime);
             IsMoving = true;
             PlayerAnimations.SetBool(moveHash, IsMoving);
         }
         if(Input.GetKey(Left))
     
         {
             transform.Rotate(-Vector3.up * 30 * Time.deltaTime);
             IsMoving = true;
             PlayerAnimations.SetBool(moveHash, IsMoving);
         }
             
         if(Input.GetKey(Right))
         {
             transform.Rotate(Vector3.up * 30 * Time.deltaTime);
             IsMoving = true;
             PlayerAnimations.SetBool(moveHash, IsMoving);
         }
         else
         {
             IsMoving = false;
             PlayerAnimations.SetBool(moveHash, IsMoving);
         }
     
         //If the left mouse button is clicked transition into attack animation
         if(Input.GetMouseButtonDown(0))
         {
             PlayerAnimations.SetBool(attackHash, CanAttack);
         }
         else
         {
             CanAttack = false;
         }
     }

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
0
Best Answer

Answer by saschandroid · Aug 01, 2016 at 07:29 PM

I didn't test if this solves you problem but you are using 'if/else' in the wrong way. With else in line 31 you are setting IsMoving = false every time you are not pressing the 'Right'-key.

Change your code to something like:

     void Update()
      {
          IsMoving = false;
          //If one of the 4 movement keys are hit transition into moving animation or if not go back to idle
          if (Input.GetKey(Forward))                 
          {
              transform.Translate(Vector3.forward * 5 * Time.deltaTime);
              IsMoving = true;
          }
          if(Input.GetKey(Backward))
          {
              transform.Translate(Vector3.back * 5 * Time.deltaTime);
              IsMoving = true;
          }
          if(Input.GetKey(Left))
      
          {
              transform.Rotate(-Vector3.up * 30 * Time.deltaTime);
              IsMoving = true;
          }
              
          if(Input.GetKey(Right))
          {
              transform.Rotate(Vector3.up * 30 * Time.deltaTime);
              IsMoving = true;
          }
          PlayerAnimations.SetBool(moveHash, IsMoving);
         //If the left mouse button is clicked transition into attack animation
          if(Input.GetMouseButtonDown(0))
          {
              PlayerAnimations.SetBool(attackHash, CanAttack);
          }
          else
          {
              CanAttack = false;
          }
      }


Comment
Add comment · Show 1 · 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 Trevdevs · Aug 01, 2016 at 07:44 PM 0
Share

Correction that actually was the issue for the moving one, can't believe I didn't think of that knowing how long ive been using if else statements.

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

52 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

Related Questions

UnityEngine.AnimatorStateInfo.nameHash' is obsolete: "Use AnimatorStateInfo.fullPathHash instead." 0 Answers

Animator HashToString returns 0 1 Answer

How to run String To Hash at compile time? 1 Answer

How to get Animator's Layer --> Sub-State --> State namehash? 2 Answers

2D Animation does not start 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