- Home /
Mecanim, detecting states in time.
Strange one. I have a character who performs a kick animation. He is controlled by Mecanim and the Animator controller. He has a slight offset which cannot be removed no matter what I do. So to get round this I use MatchTarget in an update to 'drag' him back to the position he began. This works with an unintended side effect which is both good and bad. The result is this:
While performing the kick on a loop he is succesfully dragged back to his starting place subtly so you can't tell. Perfect. However, I get a warning message saying that MatchTarget doesn't have any effect during a transition. This is good because it means my guy isn't dragged back during a kick where his root needs to move, but he IS dragged back once the transition is over. This is perfect but I want to get rid of the warning message to keep it clean.
To get rid of the message I know I need to do some sort of if statement to make the matchtarget only apply when the character is idle. This is what I came up with.
#pragma strict
var animator : Animator;
var currentBaseState : AnimatorStateInfo;
var idleState : int;
var currentState : int;
function Start () {
idleState = Animator.StringToHash("Base Layer.thai_idle_1");
}
function Update () {
currentBaseState = animator.GetCurrentAnimatorStateInfo(0);
currentState = (animator.GetCurrentAnimatorStateInfo(0).nameHash);
//if(animator.GetCurrentAnimatorStateInfo(0).IsName ("thai_idle_1"))
if(currentBaseState.nameHash == idleState)
{
animator.MatchTarget(Vector3(0,0,0), Quaternion.identity, AvatarTarget.Root, MatchTargetWeightMask(Vector3(1,1,1), 0), 0,1);
}
}
I tried a few variations on this but nothing worked - or it did half the time.
Main problem is, in the inspector during playback the hash identifier changes when I activate and deactivate the different animations, so I can see when the 'drag' should come into play. When I leave the kick looping the character now slides off like he did before - negating the whole process - BUT the drag comes into play at seemingly arbitrary times after the loop stops. The idle clip is looping away but the character is still stood away from his start point and - depending on how long he was looping for - he gets dragged back at odd times.
For example: Leave the kick loop activated for 7 loops. Character slides away to the left. Turn off Loop. Charcater remains on the left, performing the idle animation for about 14 loops. It seems to be double or more the kick-loops??? After the arbitrary number of loops he finally gets dragged back into place. BUT sometimes it works. Sometimes when the kick loop is active he kicks, resets position thanks to matchtarget, then kicks again. With no changes in code. Huh?
So ultimately I tried to clean up the errors and came up with an unpredicatble alternative that barely works. Any suggestions on alternative methods for being able to matchtarget after each loop of an animation?
To clarify - the title of this is so named because this particular problem makes it look as if the Animator is failing to recognise or relay the proper state so the character can respond despite the inspector proving that the proper state has been detected in time.