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.