首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >阻止不了Coroutine

阻止不了Coroutine
EN

Stack Overflow用户
提问于 2022-03-09 12:34:27
回答 1查看 63关注 0票数 0

伙计们,当我先给它贴上披萨标签的食物时,我怎么能阻止它呢?

请帮帮我,我找不到路。我该怎么办?顺便说一句,我是新来的,很抱歉发帖不好:/

我在做比萨饼店的游戏。所以顾客会坐着等披萨,如果我们给披萨贴标签,服务员就应该开始,然后开始吃比萨饼,但我的服务生比萨饼并没有停止。

代码语言:javascript
复制
IEnumerator EAT, PAY, WAIT;

private void Start()
{
    EAT = EatPizza();
    WAIT = WaitForPizza();
    PAY = WaitForTheCase();
}

private void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Pizza"))
    {
        canEat = true;
    }

    if (other.gameObject.CompareTag("Out"))
    {
        Destroy(this.gameObject);
    }
}

void Update()
{
    if (isEating)
    {
        if (Vector3.Distance(transform.position, agent.destination) <= 1f)
        {
            anim.SetTrigger("sit");
            isEating = false;
        }
    }

    if (!isEating && !isPaying && !isOut && !canEat)
    {
        StartCoroutine(WaitForPizza());
    }

    if (!isEating && !isPaying && !isOut && canEat)
    {
        StopCoroutine(WAIT);
        StartCoroutine(EatPizza());
    }

    if (isPaying)
    {
        isEating = false;
        anim.SetTrigger("walk");
        
        agent.destination = checkoutTarget.position;
        if (Vector3.Distance(transform.position, agent.destination) <= 1f)
        {
            anim.SetTrigger("wait");
            if (ifCashier)
            {
                //throw money 
                isPaying = false;

                isOut = true;
            }

            if (!ifCashier)
            {
                StartCoroutine(WaitForTheCase());
            }
        }
    }

    if (isOut)
    {
        anim.SetTrigger("walk");
        isEating = false;
        isPaying = false;
        
        agent.destination = outTarget.position;
    }

    if (canEat)
    {
        StopCoroutine(WAIT);
    }
}

IEnumerator WaitForTheCase()
{
    anim.SetTrigger("wait");
    yield return new WaitForSeconds(randomFloatWait);
    angry.SetActive(true);
    
    isEating = false;
    isPaying = false;
    isOut = true;
}

IEnumerator EatPizza()
{
    angry.SetActive(false);
    StopCoroutine(WaitForPizza());
    happy.SetActive(true);
    yield return new WaitForSeconds(randomFloatWait);
    
    happy.SetActive(false);
    isEating = false;
    isPaying = true;
    isOut = false;
}

IEnumerator WaitForPizza()
{
    while (!canEat)
    {
        yield return new WaitForSeconds(randomFloatWait);
        angry.SetActive(true);
    
        isEating = false;
        isPaying = false;
        isOut = true;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2022-03-30 10:57:11

首先,将WAIT定义为Coroutine,然后当您想要开始等待时使用WAIT = StartCoroutine(WaitForPizza());,当您想要完成它时使用StopCoroutine(WAIT);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71409445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档