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 Henri 1 · Mar 22, 2011 at 12:56 AM · animationloopstop

Stop animation when button is released

Hi,

I want my character to "Power Up".

Now, when you press the power up button, it plays the first animation, the loops the appropriate animation. Only problem is, the animation does not stop playing.

I want to let the loop animation play for as long as you press the button down, and when the button is released, it must stop the animation. Any ideas?

Ps. I got the script from another question in this forum.

var PowerUp : String = "PowerUp"; var PowerUpLoop : String = "PowerUpLoop";

function COPowerUp() { var state : AnimationState = animation[PowerUp];

 state.wrapMode = WrapMode.ClampForever;
 state.speed = 1.0;
 animation.Play(PowerUp);
 yield new WaitForSeconds(state.length);
 animation.Play(PowerUpLoop);
 inTransition = false; 

}

function Update () { var horizontal = Input.GetAxis("Horizontal"); var vertical = Input.GetAxis("Vertical");

 if (Input.GetButtonDown("Power Up")) {
     StartCoroutine(COPowerUp());
     }

 if (Input.GetButtonUp("Power Up")){
     StopCoroutine(COPowerUp());
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jason_DB · Mar 22, 2011 at 01:22 AM

You could use Animation.Stop right before StopCoroutine. I would also check whether or not the animation is set to loop in the import settings for the animation, which would explain why it wouldn't stop to begin with (and would also mean you wouldn't have to keep replaying it).

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 Henri 1 · Mar 22, 2011 at 03:50 PM 0
Share

Works 100%! Thanks, only prob, when you press the button quickly, and not hold it in, it still loops :( PS. I now NOTHING about this type of code, only starting to learn now.

avatar image
0
Best Answer

Answer by Statement · Mar 22, 2011 at 02:42 AM

StopCoroutine accepts a string, so you should not call it the way you do (Or I am missing out on something fun about JS again).

if (Input.GetButtonDown("Power Up")) { StartCoroutine("COPowerUp"); // <- start it by name }

if (Input.GetButtonUp("Power Up")){ StopCoroutine("COPowerUp"); // <- stop it by name }

Also, like DastardlyBanana points out, you should stop the animation with Animation.Stop, since it otherwise would remain in "PowerUp" animation. It also looks like you would want to start the "PowerUpLoop" animation.

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 Henri 1 · Mar 22, 2011 at 03:52 PM 0
Share

It makes sence, but i get the following error: "Assets/Standard Assets/Character Controllers/Sources/Scripts/Powerup2.js(8,10): BCE0089: Type 'Powerup2' already has a definition for 'COPowerUp'."

New to scripting, sorry, but thanks for the help!

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

No one has followed this question yet.

Related Questions

Stopping an animation from looping at the end? 3 Answers

how to stop animation loop? 1 Answer

Making an animation stop once it finishes (trouble with booleans) 1 Answer

Blender Animation Looping Delay Problem 2 Answers

How to get an animation inside a fbx file to loop? 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