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 $$anonymous$$ · Sep 19, 2013 at 04:17 AM · animation

Help with an animation script.

     function Update()
     {
      if(Input.GetKeyDown("w"))
      {
      animation.Play("Animwalk", PlayMode.StopAll);
      }
     }

I am creating a game. I used some simple code to create this but now, when I test it and push "w" the animation continues to play without end. I am not the best coder and am still green around the ears. If someone finds a solution(s) then please post the code in the comments section.

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

Answer by syclamoth · Sep 19, 2013 at 04:21 AM

Let me explain exactly what this code snippet does.

Every frame it checks for whether the 'w' key was first pressed that frame. That is to say, Input.GetKeyDown will only evaluate 'true' once per key-press, and only at the very beginning.

When the key gets pressed, it tells the animation component to start playing an animation. Keep in mind that this is an instantaneous command, that changes the state of the animation- the animatior component itself will happily continue playing the animation until it is finished or, depending on the looping mode, forever. The 'StopAll' command makes sure that there will only be one animation playing at a time.

Currently, your code is doing exactly what you told it to do. What is the actual intended behaviour? You could simple add a matching 'Input.GetKeyUp' command, with 'animation.Stop()' to force the animation to end at the end of the keypress, if that's what you were after.

Comment
Add comment · Show 2 · 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 $$anonymous$$ · Sep 19, 2013 at 05:08 AM 0
Share

So you mean i should add this?

 else (Input.Get$$anonymous$$eyUp ("w"))
  {
  animation.Stop ("Animwalk", Play$$anonymous$$ode.StopAll);
  }
avatar image syclamoth · Sep 19, 2013 at 05:13 AM 0
Share

Almost. As you can see from the script reference, there is no valid overload for Stop that takes those parameters- in your case, you can simply use 'animation.Stop()' and it will work just fine.

Also, you don't need the 'else' statement there. While it is very rare to have a key-down and a key-up in the same frame, you don't want to create an edge-case bug by accident (the else statement would prevent the animation from stopping immediately- you'd need to press the key 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

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

Multiple Cars not working 1 Answer

Animation stoping!! 1 Answer

Where is the problem? 0 Answers

Perform Action on frame/time of animation 1 Answer

how do i use two animations on the same object?? 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