- Home /
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.
I feel it's worth noting that both GroundAttacks and LedgeStates are children of "Attack" and "Ledge$$anonymous$$ovement" states, respectively
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.
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.
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.
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.
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.
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;
}
}
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.
Your answer
Follow this Question
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