- Home /
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");
}
} }
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.
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");
}
First off, don't post comments as answers. Secondly, give me a $$anonymous$$ute- I'm busy with the moderation queue.
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..
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.
WOrks!! GREAT!!!, Thank you very much, you are an bless man...Thanks...