Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 bibleboy4u · Jun 09, 2015 at 01:37 AM · animationspriteanimatorcontrollerspritesheet

How do I change animation states... or is there a better way?

Hi there, I have been trying to find a way to animate a spritesheet using Unity's built in animator. So far the best way I have come across is in my code below (Switching animation states using an integer). However the animation is not switching states properly. Example: When I press walk, it walks... but then it will not STOP walking. Can anyone help show me what I am doing wrong here... or if they know of a better way to animate sprites in script the help would be appreciated:D Also, thanks in advanced for any help!

(Please note: I would like to keep variables such as walk speed separate from the animator parameters.)

Complete Character Controls Script:

 //Player Controls Script:
 //-----------------------
 
 //----------------------
 //Public Variables
 //----------------------
 
 var walkSpeed                    : float = 4.0;        //Walking Speed
 var runSpeed                    : float = 4.0;        //Runninging speed
 var fallSpeed                    : float = 2.0;        //Character fall speed
 var jumpHeight                    : float = 10.0;        //How high the character can jump
 var gravity                        : float = 20.0;        //Gravity
 var startPos                    : float = 0.0;
 
 //--------------------
 //Is/Enabled Variables
 //--------------------
 
 private var crouchEnabled        : boolean = true;
 private var jumpEnabled            : boolean = true;
 private var idleEnabled            : boolean = true;
 
 private var isCrouching            : boolean = false;
 private var isJumping            : boolean = false;
 private var isWalking            : boolean = false;
 
 //---------------------
 //Storage Variables
 //---------------------
 
 var moveDirection                : int = 1;        //Dictates which direction the character faces when the game starts
 
 private var velocity            : Vector3 = Vector3.zero;    //Stores numbers
 private var afterHitForceDown    : float = 1.0;                //After it collides, force the player down variable
 
 //---------------------------------
 //Player Animation State Variables
 //---------------------------------
 
 private var state_Idle        : int = 0;
 private var state_Walk        : int = 1;
 private var state_Jump        : int = 2;
 private var state_Fall        : int = 3;
 private var state_Crouch    : int = 4;
 
 //--------------------------
 //Animation Variables
 //--------------------------
 private var currentAnimationState    : int =    state_Idle;
 var anim                            : Animator;
 
 function Start(){
     anim = GetComponent("Animator");
 }
 
 function Update(){
 
 //---------------------------
 //Getting components section:
 //---------------------------
 
 var aniPlay = GetComponent("Animator");
 var controller : CharacterController = GetComponent(CharacterController);    //Gets/Imports the character controller component- NEEDS ONE CONNECTED!
 //----------End Of Section---------------
 
 //---------------------------------------------------
 //Character movement, crouch, dive, and jump section:
 //---------------------------------------------------
 
 if(controller.isGrounded){
     velocity = Vector3 (Input.GetAxis ("Horizontal"), 0, 0);    //Moves the character controller horizontally - CHECK: EDIT/PROJECT SETTINGS/INPUT
     
     //-----------
     //Idle
     //-----------
     
     if(velocity.x == 0 && !isCrouching && idleEnabled){
         isWalking = false;
         ChangeState(state_Idle);
     }
     
     //--------------
     //Movement Left
     //--------------
     
     if(velocity.x < 0){            //Walking animation left
         velocity *= walkSpeed;            //Applies walking speed
         ChangeState(state_Walk);    //Plays Walking animation left
         idleEnabled = false;
         isWalking = true;
     }
     else{
         idleEnabled = true;
         isWalking = false;
     }
     
     //---------------
     //Movement Right
     //---------------
     
     if(velocity.x > 0){            //Walking animation right
         velocity *= walkSpeed;    //Applies walking speed
         ChangeState(state_Walk);//Plays Walking animation right
         idleEnabled = false;
         isWalking = true;
     }
     else{
         idleEnabled = true;
         isWalking = false;
     }
     
     //--------------
     //The Jump Move
     //--------------
     
     if(Input.GetButton ("Jump") && jumpEnabled && !isCrouching){
         isJumping = true;
         crouchEnabled = false;
         velocity.y = jumpHeight;    //This causes the player to jump
         ChangeState(state_Jump);    //Plays jumping up animation
     }
     else if(!isJumping){
         crouchEnabled = true;
         jumpEnabled    = true;
         
         isCrouching = false;
         isJumping = false;
     }
     
 }
 
 //-----------------
 //Movement in air
 //-----------------
 
 if(!controller.isGrounded){
     velocity.x = Input.GetAxis("Horizontal");//This allows the player to move while in air/jumping
     velocity.x *= runSpeed;
 }
 
 //---------
 //Falling!
 //---------
 
 if(!controller.isGrounded && controller.velocity.y < 0){
     ChangeState(state_Fall);//Plays falling down animation
 }
 
 //-----------------
 //The crouch move
 //-----------------
 
 if(velocity.x == 0 && Input.GetAxis("Vertical") < 0 && controller.isGrounded && crouchEnabled && !isJumping){
     isCrouching = true;
     jumpEnabled = false;
     ChangeState(state_Crouch);//Plays crouching animation
 }
 else if(!isCrouching){
     crouchEnabled = true;
     jumpEnabled    = true;
     
     isCrouching = false;
     isJumping = false;
 }
 
 
 //-----------End Of Section----------------------
 
 //----------------------------------------------
 //Stores which direction the player is facing:
 //----------------------------------------------
 
 if(velocity.x < 0){    //Character facing left
     moveDirection = 0;
 }
 
 if(velocity.x > 0){    //Character facing right
     moveDirection = 1;
 }
 //---------End Of Storing Direction--------------
 
 //----------------------------
 //Controller Collision Center
 //----------------------------
 
 //--------------------------------------------------
 //Collide with roof (Above), aftger hit force down
 //--------------------------------------------------
 if(controller.collisionFlags == CollisionFlags.Above){
     velocity.y = 0;
     velocity.y -= afterHitForceDown;    //Applies downward force to player
 }
 //-----------End of Secction--------------------
 
 //---------------------------
 //Necessary Stuff
 //---------------------------
 velocity.y -= gravity * Time.deltaTime;        //Applies gravity
 controller.Move(velocity * Time.deltaTime);    //Moves the controller
 }
 
 //-----------------------
 //Change State Function
 //-----------------------
 
 function ChangeState(State : int){
 if (currentAnimationState == State)
         return;
  
         switch (State) {
  
                case state_Idle:
                 anim.SetInteger ("State", state_Idle);
                 break;
                 
             case state_Walk:
                 anim.SetInteger ("State", state_Walk);
                 break;
             
             case state_Jump:
                 anim.SetInteger ("State", state_Jump);
                 break;
             
             case state_Fall:
                 anim.SetInteger ("State", state_Fall);
             
             case state_Crouch:
                 anim.SetInteger ("State", state_Crouch);
                 break;
  
         }
 }



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
3
Best Answer

Answer by Mark Gossage · Jun 09, 2015 at 06:31 AM

Wow, thats complex! Seriously, you are making matters too hard for yourself.

There is a great video http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers which gives an example, and the code is a lot simpler (read: easier to maintain) than yours currently is.

Looking at the code, it looks like you have too many bool variables. I'm guessing (I have not tried to run the code), that with so many variables they are not always being reset properly. If I had to guess, I think its either the idleEnabled or isWalking, because you seem the set them every cycle. If you look at the code you have:

 if(velocity.x < 0){...     }
      else{
          idleEnabled = true;
          isWalking = false;
      }
      
      
      if(velocity.x > 0){ ... }
      else{
          idleEnabled = true;
          isWalking = false;
      }

So it VERY likely that these two are going to be set.

To fix this:

  • Set those enabled variables to public (FOR NOW), so you can easily view them in the inspector. I'm guessing that they are not being reset properly (you can probably lose at least half of the variables and skill have it work correctly) OR

  • Watch the video and redesign your script again

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 bibleboy4u · Jun 09, 2015 at 11:55 AM 0
Share

Thank you so much! I had not thought about making them public to debug the script... Also, that video sounds great! I'll give it a shot. Thanks again!

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

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

Related Questions

add animation frames to existing spritesheet with animations? 0 Answers

Quickly switch sprite animation 2 Answers

Change entire spriteSheet from animation tree 2 Answers

Setting animator parameter on a single instance of a prefab sets the parameter for all instances 3 Answers

How to sync multiple animators with sprite animations ? 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