You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity3D跳跃延迟代码失效求助:仍可连跳爬墙

解决Unity3D跳跃延迟无效及连跳爬墙问题

针对你的跳跃延迟未生效、玩家仍可连跳爬墙的问题,可从以下几个方面修改代码:

  • 修正跳跃输入检测逻辑
    你当前使用Input.GetButton("Jump")会在按住跳跃键时持续返回true,这会导致即使cantJump暂时被设为true,松开再按住仍能触发连跳。应改为单次触发的Input.GetButtonDown("Jump"),确保只有按下瞬间才触发跳跃判断:

    if (!cantJump && (controller.collisionFlags & CollisionFlags.Below) != 0)
    {
        if (!mobileMode)
        {
            motor.inputJump = Input.GetButtonDown("Jump"); // 改用GetButtonDown
        }
        else
        {
            motor.inputJump = mobileJumping; // 确保mobileJumping是触摸按下瞬间的单次触发值,而非持续触摸状态
        }
    }
    else
    {
        motor.inputJump = false; // 不能跳跃或不在地面时,强制重置跳跃输入
    }
    
  • 调整跳跃延迟的触发时机
    仅在OnLand中启动延迟存在漏洞:比如玩家跳跃后碰到墙壁(未落地),OnLand不会触发,cantJump始终为false,仍能触发爬墙连跳。应该在跳跃动作发起的瞬间就启动延迟,而不是等落地:

    // 假设你有处理跳跃执行的代码段,在确认跳跃触发时添加以下逻辑
    if (motor.inputJump)
    {
        motor.ExecuteJump(); // 你的跳跃执行方法
        StartCoroutine(JumpDelay()); // 立即启动跳跃延迟
    }
    
  • 验证OnLand函数的触发条件
    检查OnLand是否会在玩家碰到墙壁(CollisionFlags.Sides)时误触发,如果是,需要限制OnLand仅在玩家真正接触地面(CollisionFlags.Below)时才调用,避免提前重置cantJump。

  • 完善JumpDelay的鲁棒性
    为防止重复启动协程导致逻辑混乱,可在启动前先停止已有协程:

    IEnumerator JumpDelay()
    {
        cantJump = true;
        yield return new WaitForSeconds(jumpingDelay);
        cantJump = false;
    }
    
    // 启动协程时先停止旧的
    if (jumpingDelay > 0)
    {
        StopCoroutine(JumpDelay());
        StartCoroutine(JumpDelay());
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 14:20:27