我怎样才能检测长preSS然后激活动画? [英] How can I detect a long press then activate an animation?

查看:155
本文介绍了我怎样才能检测长preSS然后激活动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能检测到触摸屏上的长preSS然后激活动画?和手指被释放时,流畅地播放另一个动画?让我们例如在天天过马路的主角:当用户presses下来,鸡下蹲,当用户释放他们的手指,鸡跳跃。同一个概念。 C#。

How can I detect a long press on a touch screen then activate an animation? And when the finger is released, play another animation smoothly? Let's take for example the main character in Crossy Road: when the user presses down, the chicken "squats" and when the user releases their finger, the chicken jumps. Same concept. C#.

推荐答案

您可以这样做多种方式,但一个简单的方法,你可以尝试为

You can do this in multiple ways but an easy way you can try is

Coroutine longPressCoroutine = null;
bool longPress = false;
void Update () {

    if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    {
        longPressCoroutine = StartCoroutine(PlayerAnimationAfterTime());
    }
    else if(Input.GetTouch(0).phase == TouchPhase.Ended)
    {
        if(longPressCoroutine != null)
        {
            StopCoroutine(longPressCoroutine);
            // if you want to play second animation only in case of longpress then use this 
            //check otherwise just play your animation here;
            if(longPress)
            {
                //play your second animation here
            }
            longPress = false;
        }
    }
}

IEnumerator PlayerAnimationAfterTime()
{
    yield return new WaitForSeconds(2f);
    //Play your first animation here;
    longPress = true;
}

这篇关于我怎样才能检测长preSS然后激活动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆