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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by weinster · Feb 02, 2013 at 01:11 AM · animationcharactercontrollerinput.getaxis

Input.GetAxis Animation Problem

I have been wrestling with this issue for a few days now and id really like some help to resolve it. I have a series of animations and for some reason one of them will not play. Here is my code and ill explain my problem after...

 using UnityEngine;
 using System.Collections;
 
 public class AnimationTest : MonoBehaviour {
 
     public float runSpeed;
     public float gravity;
     public int lookDirection = 1;                       // 1 is right, 0 is left
     
     Vector2 velocity          = Vector2.zero;
     
     tk2dAnimatedSprite anim;                            //2dToolkit Animated Sprite
         
     void Start () 
     {
         anim = GetComponent<tk2dAnimatedSprite>();    
     }
             
     void Update () 
     {
         
         CharacterController player = GetComponent < CharacterController > ();
         
         if (player.isGrounded) 
         {
             velocity = new Vector2 (Input.GetAxis ("Horizontal"),0);
             IdleAndRun();
             Crouch();
         }
             
         WhereAmILooking();                                 //controls lookDirection
         velocity.y -= gravity * Time.deltaTime;
         player.Move (velocity * Time.deltaTime);
         
     }
     
     void IdleAndRun ()
     {
         if (velocity.x == 0 && lookDirection == 1)                                    
         {
         if (!anim.IsPlaying("Idle"))anim.Play("Idle");
                 anim.animationCompleteDelegate = null;        
                 print ( "Idle Right" );                                
         }
         if (velocity.x == 0 && lookDirection == 0)                                    
         {
         if (!anim.IsPlaying("Idle Left"))anim.Play("Idle Left");
                 anim.animationCompleteDelegate = null;        
                 print ( "Idle Left" );                        
         }    
         if (velocity.x > 0  && lookDirection == 1)                                                            
         {
         if (!anim.IsPlaying("Run"))anim.Play("Run");
                 anim.animationCompleteDelegate = null;                                    
                 velocity *= runSpeed;    
                 print ( "Running Right" );
         }
         if (velocity.x < 0  && lookDirection == 0)                                                            
         {
         if (!anim.IsPlaying("Run Left"))anim.Play("Run Left");
                 anim.animationCompleteDelegate = null;                                
                 velocity *= runSpeed;
                 print ( "Running Left" );
         }
     }
     
     void Crouch () 
     {
         if (velocity.x == 0 && lookDirection == 1 && Input.GetAxis ( "Vertical") < 0)
         {
             if (!anim.IsPlaying ("Crouch"))anim.Play("Crouch");
             anim.animationCompleteDelegate = null;
             print ("Crouching");
         }
         if (velocity.x == 0 && lookDirection == 0 && Input.GetAxis ( "Vertical") < 0)
         {
             if (!anim.IsPlaying ("Crouch Left"))anim.Play("Crouch Left");
             anim.animationCompleteDelegate = null;
             print ("Crouching Left");
         }    
     }
     
     void WhereAmILooking ()
     {
         if (velocity.x > 0)                                                            
         {
             lookDirection = 1;                                                        
         }
         if (velocity.x < 0)                                                            
         {
             lookDirection = 0;                                                        
         }    
     }
 }

My problem is that the 'idle' and 'run' animations in both directions work just fine. The 'crouch' animation, however, halts at the first frame. If i replace either the 'idle' or 'run' animation with a 'crouch' animation, the 'crouch' animation will play. Now, if i replace the 'crouch' animation with a 'run' or 'idle', they will not play but halt on the first frame. My problem has to be my conditions for calling this animation. What i have found is that if the player is grounded, it will only play animation that are controlled by the right or left arrows. I have tried all combinations of Input.GetKey, Input.GetButton, and Input.GetAxis and none of these methods work. Where am i going wrong? Why will the animations only play with the right or left arrow?

I'm pretty new to unity so i feel like i must be missing something simple. Thank you to anyone who can help me out.

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

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to export MMD model to Unity? 0 Answers

Animation problem with rotation and position. 0 Answers

Adding a Controller, and setting up animations on a Prefab? 1 Answer

Collisions with an animated character 1 Answer

Root motion / How to bake into pose Root Transform Position (x) 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