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 RichCoggin · Dec 03, 2013 at 04:09 PM · animationif-statementsanimationclip

if statements for animations js

Hey there,

I'm stuck trying to order my animations correctly. I;ve tried lots of ways to cut it, but can't get to work I'm trying to create various states for various actions such as :

When running, colliding with an object, when jumping, when in air colliding with an object and also a death animation.

Here's the code so far:

 ///For Character Animation
 
 var isHurt = false;
 var isDead = false;
  
 function Update(){
  
   var moveDir = transform.forward * speed; // calculate the horizontal speed
   
   if (!cc) cc = GetComponent(CharacterController); // get the CharacterController
   if(cc.isGrounded) {
 // when grounded...
     vSpeed = 0.0;
 
 //For stumble
       if (isHurt == true) {
 ///THIS DOESN'T     
               if (isDead == true) {
             Debug.Log("Animate Death");
               Peter_Run15.animation.CrossFade("Idle",0.15f);  
               }
 ///THIS WORKS              
       Debug.Log("Animate Stumble");
       Peter_Run15.animation.CrossFade("Idle",0.15f);
       TurnoffHurt();  
       }
 /// THIS WORKS     
 //For Jump
       if (Input.GetButton ("Jump")) {
               vSpeed = jumpSpeed; 
               Peter_Run15.animation.CrossFade("Jump",0.15f);
               }
 ///THIS WORKS
 //For Run
       else {if (isHurt == false) {
               //Animation code for the run
               Peter_Run15.animation.CrossFade("Run",0.15f);
               }
       }
       
 }
 
     if(!cc.isGrounded) {
 ///THIS DOESN'T WORK    
     if(isHurt == true) {
       Debug.Log("Animate Stumble");
       Peter_Run15.animation.CrossFade("Stumble",0.15f);
       TurnoffHurt();  
       }
  /// NOT SURE IF THIS WORKS     
        else {if (isHurt == false) {
       //Animation code for the run
       Peter_Run15.animation.CrossFade("Jump",0.15f);
       }}
       
      }
       
   vSpeed -= gravity*Time.deltaTime; // apply a physically correct gravity
   moveDir.y = vSpeed; // add the vertical speed
   cc.Move(moveDir*Time.deltaTime); // and finally move the character
 }
  
 
 ///ALL THIS THE BELOW IS WORKING; TURNING ON AND OFF variables
  ///Function to turn off Hurt trigger for animation
  function TurnOnHurt(){
          isHurt = true;
          Debug.Log("isHurt Turned ON");
          }
  
  function TurnoffHurt(){
         yield WaitForSeconds(0.480); // time in Seconds how long your stumble animation is or for how long you want to wait until this animation can be played again.
         isHurt = false;
         Debug.Log("isHurt Turned OFF");
         } 
         
 //Function for dead animation
 
 function StartDeadAnimation(){
           isDead = true;
           Debug.Log("isDead Turned ON");
           }  

Any help would be much appreciated.

Cheers,

Rich

Comment
Add comment · Show 2
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 apple92 · Dec 13, 2013 at 12:50 PM 0
Share
 if (isHurt == true) {
 ///THIS DOESN'T     
          if (isDead == true) {
             Debug.Log("Animate Death");
 20.         Peter_Run15.animation.CrossFade("Idle",0.15f);  
          }
 ///THIS WOR$$anonymous$$S           
       Debug.Log("Animate Stumble");
       Peter_Run15.animation.CrossFade("Idle",0.15f);
 25.      TurnoffHurt();  
       }


If you change this to :

 if (isHurt == true) {
    if(isDead == false) {
          Debug.Log("Animate Stumble");
          Peter_Run15.animation.CrossFade("Idle",0.15f);
          TurnoffHurt();  
     }
     else {
      Debug.Log("Animate Death");
      Peter_Run15.animation.CrossFade("Idle",0.15f); 
     }
 }

what happens?

Can't really see a problem here except that at the moment your Death Animation always gets overwritten by the stumble Animation.

Debug.Log should still print "Animate Death"...

avatar image KellyThomas · Dec 13, 2013 at 01:10 PM 0
Share
  • Are both of the death/hurt states playing "Idle"?

  • It looks like they can jump even when isDead is true

  • If you use consistent indenting you will have an easier time scanning and modifying this code (use your IDE to autoformat if you prefer)

  • you have redundant checks on your elses, the else block will execute only when the if condition if false:

    if(isHurt == true) { Debug.Log("Animate Stumble"); Peter_Run15.animation.CrossFade("Stumble",0.15f); TurnoffHurt();
    } /// NOT SURE IF THIS WOR$$anonymous$$S
    else {if (isHurt == false) { //Animation code for the run Peter_Run15.animation.CrossFade("Jump",0.15f); }}

is equivalent to:

     if(isHurt) {
         Debug.Log("Animate Stumble");
         Peter_Run15.animation.CrossFade("Stumble",0.15f);
         TurnoffHurt();  
     }
  /// NOT SURE IF THIS WOR$$anonymous$$S     
     else {
         //Animation code for the run
         Peter_Run15.animation.CrossFade("Jump",0.15f);
     }

  • TurnOffHurt will wait for 0.48 secs before setting isHurt to false, if your game is running at 60 fps this might result in 0.48 * 60 = 28 copies of TurnOffHurt stacked up before isHurt changes value.

0 Replies

· Add your reply
  • Sort: 

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

18 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I call an animation via script? 1 Answer

Animator Override Controller changed at runtime doesn't always play the animations correctly 1 Answer

Timeline and AnimationClip problem -1 Answers

Adding a playing animation onto a transform (with a transformation) 1 Answer

Remove unused animation curves? (Animation.Update() optimizing) 1 Answer


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