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 StigC · Jul 13, 2014 at 07:37 PM · animatorgetkeydowngetkey

Stop animation after .5 seconds?

Hi there!

When pressing E, the player starts an Attack animation. That needs to stop after 0.5 seconds.

How do I do that?

The simple code looks like this:

         if(Input.GetKey(KeyCode.E))
         {
             anim.SetBool("Attack", true);
             Instantiate(meleePrefab, new Vector3(target.position.x+1,transform.position.y,target.position.z), transform.rotation);
         }
 

Thanks

Comment
Add comment · Show 6
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 DoctorSauce · Jul 13, 2014 at 07:54 PM 0
Share

This might suck, but you can try it! I don't script with animations much:

     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.E))
             {
                 anim.SetBool("Attack", true);
                 Instantiate(meleePrefab, new Vector3(target.position.x+1,transform.position.y,target.position.z), transform.rotation);
                 yield return new WaitForSeconds(0.5);
                 anim.SetBool("Attack", false);
             }

Basically just tells the script to pause for half a second (during which time the animation will be playing) then to stop the animation. Let me know if it works! If it doesn't, I'll take down my answer so you're more likely to get another one.

avatar image Firedan1176 DoctorSauce · Jul 13, 2014 at 09:55 PM 0
Share

Yeah you'd probably need to do a coroutine.

avatar image StigC DoctorSauce · Jul 13, 2014 at 09:56 PM 0
Share

I already tried this, didn't work :/ I might be wrong, but isn't the yield function only for IEnumerators ?

avatar image DoctorSauce DoctorSauce · Jul 13, 2014 at 10:32 PM 0
Share

It's possible that that's true. $$anonymous$$aybe you need to find another way to start the animation. I think if you make it a legacy animation ins$$anonymous$$d, I think there is a line that calls only a certain portion of the animation, rather than set a Bool to True.

But, that probably won't work. $$anonymous$$aybe you could duplicate the actual animation file, then go into the file and edit the animation clip to only include that 0.5 seconds of animation. Then just use that new clip of 0.5 seconds as the animation to set to true.

avatar image Nick4 · Jul 13, 2014 at 07:58 PM 0
Share

You need to specify which language you are using so we can be more helpful.

avatar image StigC · Jul 13, 2014 at 09:57 PM 0
Share

Sorry about that, it's C#

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Addyarb · Jul 18, 2014 at 07:40 PM

 void Update(){
 
   if(Input.GetKey(KeyCode.E))
     {
         anim.SetBool("Attack", true);
         StartCoroutine(WaitForHalfASecond());
         Instantiate(meleePrefab, new Vector3(target.position.x+1,transform.position.y,target.position.z), transform.rotation);
     }
 }
     
     IEnumerator WaitForHalfASecond(){
         yield return new WaitForSeconds(0.5);
         anim.SetBool("Attack", false);
     }

C#

If you have any questions, or would like an explanation on how to further use IEnumerators in this case, I will be happy to answer in the comments. IEnumerating is a difficult concept to grasp until you've done it a lot!

Comment
Add comment · 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

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

7 People are following this question.

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

Related Questions

GetKey/GetKeyDown, Paired with Adjusting Stats 1 Answer

Can/how do I set up a boolean to continue an animation after button press 1 Answer

How to get holding key to prioritize 1 Answer

Check if key is pressed once 1 Answer

How to stop repeated letter/backspace entry in an input field when they key is held down? 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