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 ohlin · Jun 22, 2014 at 08:57 PM · sprite animation

how to have animator play sprite animation on button press. wait for completion.

. So my set up is i have various animations for character movements. i have a animator controller set up to play them depending on the value of a variable. i then have code where when a button is pressed, depending, variable mvar is changed.

. Im trying to get my character to jump now. So I tried to code it to where the when the space key is pressed, it changes the mvar value. Which tells the animator controller to play the jump animation. Then I wait a couple seconds and change the value back to 0 telling the controller to play the standing animation. I cant get thing to work. I dont know how I would get this to work. Please Point me in the right direction. Everything I see is for 3d animations and this is 2d... . The debug messages works. it waits. but the jump animation flashes for split second before returning to standing animation. Ive messed with wait times up to 8 seconds with no luck. I think I am not using appropriate code. How to write it to wait for the animations completion?

heres my code:

 public class FSpriteCntrl : MonoBehaviour {
     private Animator animator;
     int keyw;
     int keya;
     int keys;
     int keyd;
 
 
 
     void Start () {
 
         animator = this.GetComponent<Animator>();
     }
 
     void Update () {
 
         //sets values if any movemnt button is pressed
         if (Input.GetKeyDown ("w")) {
             keyw = 1;
             animator.SetInteger ("MVar",1);
         }
         if (Input.GetKeyDown ("a")) {
             keya = 1;
             animator.SetInteger ("MVar",2);
         }
         if (Input.GetKeyDown ("s")) {
             keys = 1;
             animator.SetInteger ("MVar",3);
         }
         if (Input.GetKeyDown ("d")) {
             keyd = 1;
             animator.SetInteger ("MVar",2);
         }
         if (Input.GetKeyDown (KeyCode.Space)) {
                 
             StartCoroutine(Jump());  //when space key is pressed runs the subroutine.
         }
 
         //when wasd are let go sets the key variable to 0
         if (Input.GetKeyUp ("w")) {
             keyw = 0;
         }
         if (Input.GetKeyUp ("a")) {
             keya = 0;
         }
         if (Input.GetKeyUp ("s")) {
             keys = 0;
         }
         if (Input.GetKeyUp ("d")) {
             keyd = 0;
         }
         
         //checks wasd if none are being held then sets variable to play stand animation
         if (keyw == 0 && keya == 0 && keys == 0 && keyd == 0){        
             animator.SetInteger ("MVar", 0);
         }
         
 
     }
 
     IEnumerator Jump() {
 
         animator.SetInteger ("MVar", 5); //sets mvar to 5 telling animator controller to play the sprites jump animation.
         Debug.Log("Before Waiting 1 seconds");
         yield return new WaitForSeconds(1); 
         animator.SetInteger ("MVar", 0);//changes value back to zero so the character returns to standing animation.
         Debug.Log("After Waiting 1 Seconds");
     }
 
     
 }
 
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

2 People are following this question.

avatar image avatar image

Related Questions

Dependency sprite animation from the variable 0 Answers

2D Sprite animation: Mesh swap or UV swap? 1 Answer

Sprite error while changing Animation clips 0 Answers

Wanting to have a sprite animation to only start playing once the player collides with it? 1 Answer

How do I check, and change, the current frame/time of a sprite animation in Unity 4.3? 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