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 allfader · Jun 17, 2013 at 06:14 PM · animationjavascriptidle

Playing a second idle animation...

Im playing around with the Constructor controllerscript and was wondering if it is possible to add a second idle animation to it. Lets say a robot has been stationary for a few seconds and starts the idle animation, the after a certain amount of time it starts a shutdown animation which is not looped. Any ideas on how I can do this when I cant seem to get it to work without messing up the idle animation.

 #pragma strict
 var charController:CharacterController ;
 /* Create a variable of type CharacterController to store
 our component and call it in the script                */
 var walkSpeed : float = 1 ;
 var runSpeed : float = 1.8 ;
 var rotationSpeed : float = 250 ;
 var jumpForceDefault : float = 2 ;
 var cooldown : float = 5 ;
  
 private var jumpForce : float;
 private var gravityPull : float = 1;
 /* Jump action related costants                         */
  
  
 private var isRunning : boolean ;
 private var isWalking : boolean ;
 private var isStrafing : boolean ;
 private var isJumping : boolean ;
 private var isAttacking : boolean ;
 private var isIdle : boolean ;
 /* Create boolean status variables to identify animation
 status, e.g. what am i doing right now?               */
  
 function Start (){
 var cc : CharacterController;
 cc = gameObject.AddComponent("CharacterController");
 /*  Adds a Character Controller component to gameobject */
  
 charController = GetComponent(CharacterController);
 /*    Assigns it in the charController variable to use it */
  
 // Set all animations to loop
 animation.wrapMode = WrapMode.Loop;
 // except shooting
 animation["attack"].wrapMode = WrapMode.Once;
 
 // Put idle and walk into lower layers (The default layer is always 0)
 // This will do two things
 // - Since shoot and idle/walk are in different layers they will not affect
 //   each other's playback when calling CrossFade.
 // - Since shoot is in a higher layer, the animation will replace idle/walk
 //   animations when faded in.
 animation["attack"].layer = 1;
 
   
 // Stop animations that are already playing
 //(In case user forgot to disable play automatically)
 animation.Stop();
  
  
 }
  
  
  
 function Update(){
  
 charController.Move(transform.up * Time.deltaTime * -gravityPull * 1);
  
 /* Gravity */
  
 if(Input.GetAxis("Vertical") > 0){
 /*    If the Vertical input axis is positive (by default by
 pressing W or up arrow)                                */
 if(Input.GetButton("Vertical")){
 isRunning = true ;
 animation["run"].speed = 4;
 animation.CrossFade("run");
 /*    While Run button is pressed play run animation, with
 Crossfade try to blend nicely different animations )*/
 charController.Move(transform.forward*Time.deltaTime*runSpeed)    ;
 /*    While Run button is pressed move faster !)          */
 /*    Use the Move function, Time.deltatime makes things go
 equally fast on different hardware configurations,
 by moving in the forward direction with walkspeed   */
 isRunning = true ;
 /* Set the isRunning flag as true since i am running    */
 
 /*  Tell me what i am doing now                         */
 }
 else{
 isWalking = true ;
 /* Else if i am moving forward and not running i walk   */
 animation["walk"].speed = -1;
 animation.CrossFade("walk", 0.9);
 /*    While walk button is pressed play walk animation ! */
 charController.Move(transform.forward*Time.deltaTime*walkSpeed)    ;
 
 /*  Tell me what i am doing now                         */
 }
 }
 else if(Input.GetAxis("Vertical") < 0){
 isWalking = true ;
 /* Do the same for the back direction, no back run!    */
 animation["walk"].speed = -1.5;
 /* revert walk animation playback                        */
 animation.CrossFade("walk");
 charController.Move(transform.forward*Time.deltaTime*-walkSpeed/2)    ;
 /* Move function but in the opposite to forward
 direction by using a negative (-) vector            */
 
 /*  Tell me what i am doing now                        */
 }
 else{
 isWalking = false ;
 isRunning = false ;
 /* if not running or walking set these states as false */
 }
  
 if(Input.GetButtonDown("Jump") && !isJumping){
 jumpForce = jumpForceDefault ;
 isJumping = true ;
 animation.Play("jump_pose") ;
 /* Capture Jump input and prevent double air jump with
 && !isJumping, makes these lines working only while
 not already in a Jump.                               */
 }
  
 if(isJumping){
 charController.Move(transform.up * Time.deltaTime * jumpForce);
 jumpForce -= gravityPull ;
 Debug.Log("isJumping value is" + " " + isJumping);
 /* If isJumping is true (i am in Jump state), move the
 character up with jumpForce intensty, then gravityPull
 kicks in and will take you on the ground.          */
 if(charController.isGrounded){
 isJumping = false ;
 /* Check if the character is touching the ground with
 Unity default isGrounded function, if its grounded
 end the Jumping action by setting isJumping false  */
 }
 }
  
 if(!isWalking && !isRunning && !isJumping){
 animation.CrossFade("idle", 0.8);
 }
 /* If i am not doing any action , play the idle anim   */
   
 if(Input.GetAxis("Horizontal") > 0){
 charController.transform.Rotate(Vector3.up * Time.deltaTime * 20 *  rotationSpeed, Space.World);
 if(!isWalking && !isRunning && !isJumping){
 animation["turnright"].speed = -1.9;
 animation.CrossFade("turnright", 0.4); 
 }}
 
 if(Input.GetAxis("Horizontal") < 0){
 charController.transform.Rotate(Vector3.up * Time.deltaTime * 20 * -rotationSpeed);
 if(!isWalking && !isRunning && !isJumping){
 animation["turnright"].speed = 1.9;
 animation.CrossFade("turnright", 0.4); 
 }}
 /* rotate the character with left and right    arrows */
   
 //charController.transform.rotation.y += Input.GetAxis("Mouse X") * Time.deltaTime * rotationSpeed ;
 /* rotate the character with the mouse                 */
  
 if(Input.GetButton("Fire1")){
 isAttacking = true ;
 slash() ;
 isAttacking = false ;
 }
 /* Play attack animation calling slash function */
  
 /* If you wish to add STRAFE command just replicate the
 code for the forward and back direction , i am not
 doing this in this character controller tutorial
 because the Constructor model is not provided
 with the strafe animation. */
  
 }
  
 function slash(){
 //animation.Play("attack") ;
 
 }
  
 
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

1 Reply

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

Answer by roojerry · Jun 17, 2013 at 06:46 PM

something like this should give you a good idea of how to handle it

 if(!isWalking && !isRunning && !isJumping && !shutDown){
     if( idleTime > maxIdleTime){
         animation.CrossFade("shutDownAnimation");
         shutDown = true;
     }
     else{
         animation.CrossFade("idle", 0.8);
        //increase idle time
     }
 }
Comment
Add comment · Show 1 · 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 allfader · Jun 17, 2013 at 08:02 PM 0
Share

Thank you..

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

15 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

Related Questions

enemy script and destroy 0 Answers

Menu Animation 2 Answers

Animation doesn't work 1 Answer

Using animations in prefabs 1 Answer

play animation when moving,and idle animation when stand still 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