【问题标题】:In unity calling a coroutine that include playing animation inside another coroutine make the animation play only 0.1 sec统一调用包括在另一个协程中播放动画的协程使动画仅播放 0.1 秒
【发布时间】:2019-06-17 17:21:00
【问题描述】:

我有一个项目,通过蓝牙从 Arduino 控制器读取输入,并根据它读取的输入播放特定动画,输入可以是 1 或 2 或 3 或其中 2 或 3 的混合,总共 7输入。

每个读数都在一个“if”语句中,然后是一个“else if”语句,用于检查下一个组合,我正在读取一个播放动画的协程,这里的问题是它只播放了 0.1 秒然后被剪切。

我尝试添加一个条件以等到动画结束然后再次阅读,但这确实有效,我将添加一个小示例

IEnumerator  ManageConnection (BluetoothDevice device)
{   
    while (device.IsReading) 
    {
        byte [] msg = device.read ();

        if (msg != null) 
        {
            string content = System.Text.ASCIIEncoding.ASCII.GetString (msg);

            string [] lines = content.Split(new char[]{'\n','\r'});

            //Add those lines to the scrollText
            if (!animationPlaying)
            {
                if (lines.Contains("123"))
                {
                    StartCoroutine(Animating(0));
                    audsrc.PlayOneShot(audioList[5]);
                    StartCoroutine(SlowDown(0.1f));
                    //StartCoroutine(ActiveEffect(5));

                    coinsCount += 1;
                }
                else if (lines.Contains("12"))
                {
                    yeahCount += 1;
                    StartCoroutine(Animating(5));
                    audsrc.PlayOneShot(audioList[4]);
                    StartCoroutine(SlowDown(0.1f)); 
                }   
            }
        }

这个是用于动画协程的:

public IEnumerator Animating(int i)
{
    animationPlaying = true;
    animator.Play(animationNames[i]);

    yield return new WaitForSeconds(1);

    animationPlaying = false;
}           

我只想让动画播放到最后,而不是仅仅 0.1 秒。

谢谢。

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    您可以在 Animator 组件上让动画一直播放到最后,而不是使用协程进行硬编码。

    here is a link to the unity docs

    【讨论】:

    • 问题是因为这将在每一帧都被调用,如果我这样做没有等待时间,下一个调用的动画将剪切前一个,想象你用一个按钮来做,然后你按下很多次,用协程等待1秒,不管你点击按钮的速度有多快,它只会在1秒后读取下一个。
    猜你喜欢
    • 2023-03-22
    • 2013-04-07
    • 1970-01-01
    • 2014-09-03
    • 1970-01-01
    • 2016-06-12
    • 1970-01-01
    • 1970-01-01
    • 2019-09-12
    相关资源
    最近更新 更多