Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Ryan-Gatts · Apr 15, 2013 at 10:37 PM · animationjavascriptmecanim

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.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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 ); }
 
 } 
Comment
Add comment · 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
0

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.)

Comment
Add comment · 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

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

12 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

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


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