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 gringofx · May 02, 2012 at 12:02 AM · animationgameobjectbuttoncollide

Help touch button

i have two animations "Idle" and "Play" when i touch the cell phone android button made in unity i have to keep pressed the button to play the animation. I want to play the animation with just one touch in the button. What is wrong with this script i need HEELP !

function Update () { if(Input.touchCount >= 0) { var touch : Touch = Input.touches[0];

 if(touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
 {
     animation["Play"].speed= 1.0;
   animation.Play("Play");
   }
   else
   {
         animation["Idle"].speed= 1.0;
   animation.Play("Idle");
 

       }
 

} }

Comment
Add comment · Show 1
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 syclamoth · May 02, 2012 at 12:21 AM 0
Share

Also, if you're going to post here a lot, learn to format your code properly in the question field. It'll make people a lot happier to answer you.

1 Reply

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

Answer by syclamoth · May 02, 2012 at 12:08 AM

Animation.Play will play the animation from the first frame when it is called. If you call it every frame like that, it'll never get past the first frame! Try putting in some kind of check to make sure you only play the animation once per touch.

The simplest solution goes a bit like this:

 function Update () {
     if(Input.touchCount >= 0) {
         var touch : Touch = Input.touches[0];
         if(touch.phase == TouchPhase.Began)
         {
             animation["Play"].speed= 1.0;
             animation.Play("Play");
             ResetAnimation(animation["Play"]);
         }
     }
 }

 function ResetAnimation(curAnim : AnimationState)
 {
     yield WaitForSeconds(curAnim.length);
     animation.Play("Idle");
 }
Comment
Add comment · Show 6 · 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 gringofx · May 02, 2012 at 12:13 AM 0
Share

Can you tell me how, please!

avatar image syclamoth · May 02, 2012 at 12:14 AM 0
Share

First off, don't post comments as answers. Secondly, give me a $$anonymous$$ute- I'm busy with the moderation queue.

avatar image gringofx · May 02, 2012 at 12:36 PM 0
Share

works perfect, but i just want to touch the button once to begin the animation, and not have to keep the finger touching the button to the animation be completed.

How can i fix that, please..

avatar image syclamoth · May 02, 2012 at 12:39 PM 0
Share

Well, that's not how I would have expected it to work given what you had in your original post... Fixing it now. Also, remember what I said about comments and answers? It's still true! I don't want to have to manually fix it every time.

avatar image gringofx · May 02, 2012 at 04:46 PM 0
Share

WOrks!! GREAT!!!, Thank you very much, you are an bless man...Thanks...

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Please help me debugging a script! 0 Answers

Animation play error. No animation attached. 3 Answers

Activating One Part of An Animation 2 Answers

Button animation state stays Pressed or Highlighted after disabling GameObject 0 Answers

What am I doing wrong with this attack script? 2 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