- Home /
Mecanim not updating script when it switches state?
So, I have run into a problem getting a script to drive changes in Mecanim for a door asset. Here's my code:
#pragma strict
//This is a script designed to drive the Mecanim Animations for a mechanised door.
//The Door has 4 states: Open (playing the open animation), Opened (posed with door open), Close (playing the close animation), and Closed (posed with door closed).
//The Parameter that drives the changes from Closed to Open and from Opened to Close is called "DoorIsOpen" and it's a boolean.
//Declaring Variables
var openedState : int;
var closedState : int;
var openBool : int;
var anim : Animator;
var currentBaseState : AnimatorStateInfo;
function Awake ()
{
//Setting Values
openedState = Animator.StringToHash("Base Layer.Opened");
closedState = Animator.StringToHash("Base Layer.Closed");
openBool = Animator.StringToHash("Base Layer.DoorIsOpen");
anim = GetComponent(Animator);
currentBaseState = anim.GetCurrentAnimatorStateInfo(0);
}
function Update ()
{
//Say "Goodbye" when closed.
if (currentBaseState.nameHash == closedState)
{
Debug.Log ("Goodbye");
}
//Open when closed and player jumps.
if (currentBaseState.nameHash == closedState)
{
if(Input.GetButton("Jump"))
{
anim.SetBool("DoorIsOpen", true);
}
}
//Say "Hello" when opened.
if (currentBaseState.nameHash == openedState)
{
Debug.Log ("Hello");
}
//Close when open and player jumps.
if (currentBaseState.nameHash == openedState)
{
if(Input.GetButton("Jump"))
{
anim.SetBool("DoorIsOpen", false);
}
}
}
The script runs great when it comes to opening the door, but once the door is open I can't get it to close. Even when the Mecanim is in a different state, it still shows the debug text "Goodbye", which should only show when it is in the closed state.
Any idea what it's deal is? Thanks.
Update: More specifically, the mecanim won't change from it's default state. In the example above, the default state was "closed". If the default state had been "opened", it would be displaying only the "Hello" debug text, not matter what animation was playing. Ditto if it were in the "open" or "close" state, no debug text would display.
Answer by Ryan-Gatts · Apr 22, 2013 at 01:12 AM
Fixed. Not sure why the initial version didn't work, but this new one does. The key difference is that I only try to interact with the parameter "DoorIsOpen" instead of trying to check states or whatever else I was thinking. Here's the new update loop:
function Update ()
{
if ( Input.GetKeyDown( KeyCode.E ) )
{
if (openBool == 0) { openBool = 1; }
else { openBool = 0; }
Debug.Log ( openBool );
}
if ( openBool == 1 ) { anim.SetBool( "DoorIsOpen", true ); }
if ( openBool == 0 ) { anim.SetBool( "DoorIsOpen", false ); }
}
Answer by lh · May 20, 2014 at 03:43 AM
The biggest reason your old script didn't work is that you set "currentBaseState=" only in the Awake function, not Update. But your latter code is more in line with how Mecanim is meant to be used--push more of the programming into the state machine. In fact, you can usually remove the "if (currentState == X)" checks from your code, and just set parameters based on how you want the animation to transition. (Though the rules in the animation controller may end up being complex.)
Your answer
Follow this Question
Related Questions
User Created Animations Not Working With Mecanim 0 Answers
How to call animations in JS 1 Answer
Controlling Mecanim through javascript? 2 Answers
Animation depending on the Int value 0 Answers
Attack Animation through Javascript 0 Answers