You need to enable JavaScript to run this app. Unity 2020.3中落地检测协程导致程序完全冻结问题求助
优惠活动
大模型
产品
解决方案
定价
更多

Unity 2020.3中落地检测协程导致程序完全冻结问题求助

故障根因
  • 协程死循环阻塞主线程:你写的LandDetect协程中while (!playermove.isGrounded)循环没有任何挂起逻辑,只要角色未落地,该循环会在当前帧无限执行,Unity主线程会被完全阻塞,无法执行下一帧的逻辑更新isGrounded状态,直接造成应用冻结。
  • 协程重复启动冗余:JumpHandler在Update中每帧调用,只要角色处于空中状态,每帧都会启动新的LandDetect协程,数百个冗余协程同时运行死循环会快速耗尽应用资源,加重卡顿。
修复方案

方案1:修正协程逻辑(原有逻辑的优化版)

新增协程运行状态标记避免重复启动,同时给循环加帧挂起逻辑,修改后完整代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerAnimationHandler : MonoBehaviour
{
    Animator animator;
    PlayerMove playermove;
    // 新增协程运行标记,避免重复启动
    private bool isDetectingLand = false;

    IEnumerator LandDetect()  
    {
        isDetectingLand = true;
        while (!playermove.isGrounded)
        {
            animator.SetBool("Midair", true);
            // 每次循环挂起一帧,等待下一帧更新isGrounded状态,避免死循环
            yield return null;
        }
        animator.SetTrigger("Land");
        animator.SetBool("Midair", false);
        // 检测结束重置标记
        isDetectingLand = false;
    }

    void Start()
    {
        playermove = GetComponent<PlayerMove>();
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        JumpHandler();
    }

    void JumpHandler()
    {
        // 只有当前没有落地检测协程运行时才启动新的
        if (!isDetectingLand)
        {
            if (!playermove.isGrounded)
            {
                if (playermove.doubleJumpOccur)
                {
                    animator.SetTrigger("DoubleJump");
                }
                else
                {
                    animator.SetBool("Midair", true);
                }
                StartCoroutine(LandDetect());
            }
            else if (playermove.jumpOccur)
            {
                animator.SetTrigger("Jump");
                StartCoroutine(LandDetect());
            }
        }
    }
}

方案2:不用协程的更轻量实现

直接通过对比上下帧的 grounded 状态判断落地时机,逻辑更简单不易出错:

using UnityEngine;

public class PlayerAnimationHandler : MonoBehaviour
{
    Animator animator;
    PlayerMove playermove;
    // 记录上一帧的落地状态
    private bool lastFrameGrounded = true;

    void Start()
    {
        playermove = GetComponent<PlayerMove>();
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        bool currentGrounded = playermove.isGrounded;
        // 上一帧在空中,当前帧落地,触发落地动画
        if (!lastFrameGrounded && currentGrounded)
        {
            animator.SetTrigger("Land");
            animator.SetBool("Midair", false);
        }
        // 上一帧落地,当前帧起跳,触发跳跃/二段跳动画
        else if (lastFrameGrounded && !currentGrounded)
        {
            if (playermove.doubleJumpOccur)
            {
                animator.SetTrigger("DoubleJump");
            }
            else if (playermove.jumpOccur)
            {
                animator.SetTrigger("Jump");
            }
            animator.SetBool("Midair", true);
        }

        // 更新上一帧状态
        lastFrameGrounded = currentGrounded;
    }
}

内容的提问来源于stack exchange,提问作者Tsubodai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.19 21:27:05