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 ToeBeanGames · Mar 06, 2017 at 09:05 PM · animationunity 5mechanim

How can I change a material only during specific animations?

Okay so I DO have it set up so it only changes the material for the duration of the animation, but I'm stuck on actually having it change to a specific material during a specific animation (i.e. one mouth material is for attacking, one for climbing, etc.). I've tried both GetCurrentAnimatorStateInfo IsName and nameHash, neither have worked so far. Here's what I have:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMouthAnimator : MonoBehaviour {
 
     public Material mouthDefault, mouthAttack, mouthLookBack;
     public Renderer mouthRenderer;
     float animTimer;
     public Animator _anim;
     static int AttackState = Animator.StringToHash ("GroundAttacks");
     static int ClimbState = Animator.StringToHash ("LedgeStates");
 
     // Use this for initialization
     void Start () {
         mouthRenderer.material = mouthDefault;
     }
     
     // Update is called once per frame
     void FixedUpdate () {
         if (_anim.GetCurrentAnimatorStateInfo (0).nameHash == AttackState) {
             animTimer = _anim.GetCurrentAnimatorClipInfo (0).Length - 0.4f;
             mouthRenderer.material = mouthAttack;
         }
         if (_anim.GetCurrentAnimatorStateInfo (0).nameHash == ClimbState) {
             animTimer = _anim.GetCurrentAnimatorClipInfo (0).Length;
             mouthRenderer.material = mouthLookBack;
         }
 
         if (animTimer > 0) {
             animTimer -= Time.deltaTime;
         }
         if (animTimer <= 0) {
             mouthRenderer.material = mouthDefault;
         }
 
         
     }
 }

But every time I test, neither of these animation states actually change the mouth material. It's starting to get a bit frustrating so any help is appreciated.

Comment
Add comment · Show 2
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 ToeBeanGames · Mar 06, 2017 at 09:07 PM 0
Share

I feel it's worth noting that both GroundAttacks and LedgeStates are children of "Attack" and "Ledge$$anonymous$$ovement" states, respectively

avatar image ToeBeanGames · Mar 07, 2017 at 02:36 AM 0
Share

Bump? Clearly there's got to be SO$$anonymous$$E solution, literally all I'm trying to do is check if the animator is in a certain state.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HarshadK · Mar 07, 2017 at 05:52 AM

Try using AnimatorStateInfo.shortNameHash instead of nameHash as you are comparing with just the state name.

Comment
Add comment · Show 8 · 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 ToeBeanGames · Mar 07, 2017 at 06:10 AM 0
Share

Still not working.

avatar image HarshadK ToeBeanGames · Mar 07, 2017 at 06:23 AM 0
Share

Check if you are entering the if statement for changing the material. If you are then check if _anim.GetCurrentAnimatorClipInfo (0) has length greater than 0. If the length of this is 0 then your animTimer becomes less than 0 and your if (animTimer <= 0) snippet executes changing the material back to default. To me, it seems to be the length issue.

avatar image ToeBeanGames HarshadK · Mar 07, 2017 at 06:26 AM 0
Share

It's not; the mouth isn't even changing during the attack animation. It worked before when I had it change the mouth when the attack button was pressed but I had to change it from that because no matter what state the player was in the mouth would be changed by pressing the attack button.

Show more comments
avatar image ToeBeanGames · Mar 07, 2017 at 06:36 AM 0
Share

I added lines to print "attacking" or "hanging" to the console before changing the material and I'm getting nothing. It's definitely an issue with the if statements.

avatar image ToeBeanGames ToeBeanGames · Mar 07, 2017 at 06:38 AM 0
Share

This is the new FixedUpdate

 void FixedUpdate () {
 
         if (_anim.GetCurrentAnimatorStateInfo (0).shortNameHash == AttackState) {
             Debug.Log ("Attacking");
             animTimer = _anim.GetCurrentAnimatorClipInfo (0).Length;
             mouthRenderer.material = mouthAttack;
         }
         if (_anim.GetCurrentAnimatorStateInfo (0).shortNameHash == ClimbState) {
             Debug.Log ("Looking Back");
             animTimer = _anim.GetCurrentAnimatorClipInfo (0).Length;
             mouthRenderer.material = mouthLookBack;
         }
 
         if (animTimer > 0) {
             animTimer -= Time.deltaTime;
         }
         if (animTimer <= 0) {
             mouthRenderer.material = mouthDefault;
         }
 
         
     }
avatar image HarshadK ToeBeanGames · Mar 07, 2017 at 07:05 AM 1
Share

Debug.Log the output of _anim.GetCurrentAnimatorStateInfo (0).shortNameHash and

 static int AttackState = Animator.StringToHash ("GroundAttacks");
 static int ClimbState = Animator.StringToHash ("LedgeStates");

This will let us know if they are actually same or not.

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

177 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

Related Questions

Imported Blender Animations not Working 2 Answers

Sometimes blend tree do not work 1 Answer

Issue with mecanim playing an animation using setbool 1 Answer

Can I Use Multiple Models In One Main Model? 0 Answers

Unity Mechanim playing default layer state animation when gameObject is duplicated in the scene 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