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

Unity启动后Animator中Jump参数持续负向递减问题求助

问题原因及修复方案

核心问题分析

  1. 地面检测失效:CheckGroundStatus方法的射线可能无法正确触达地面,导致m_IsGrounded始终为false。此时UpdateAnimator会持续将Jump参数绑定到Rigidbody.velocity.y,而角色受重力影响,velocity.y会持续负向递减,最终让Animator的Jump参数无限降低。
  2. 地面状态下未重置Jump参数:即使角色成功落地,现有代码也不会主动将Animator的Jump参数归0,会残留异常负值。

具体修复步骤

1. 修复地面检测逻辑

修改CheckGroundStatus方法,排除自身Collider干扰,同时优化射线起始位置,确保检测准确性:

void CheckGroundStatus()
{
    RaycastHit hitInfo;
#if UNITY_EDITOR
    Debug.DrawLine(transform.position + (Vector3.up * 0.1f), transform.position + (Vector3.up * 0.1f) + (Vector3.down * m_GroundCheckDistance));
#endif
    // 忽略自身Collider,避免射线误判
    int layerMask = ~(1 << gameObject.layer);
    // 从胶囊碰撞体底部发射射线,贴合角色实际接地位置
    Vector3 rayStart = m_Capsule.bounds.center - Vector3.up * m_Capsule.bounds.extents.y;
    if (Physics.Raycast(rayStart, Vector3.down, out hitInfo, m_GroundCheckDistance, layerMask, QueryTriggerInteraction.Ignore))
    {
        m_GroundNormal = hitInfo.normal;
        m_IsGrounded = true;
        m_Animator.applyRootMotion = true;
    }
    else
    {
        m_IsGrounded = false;
        m_GroundNormal = Vector3.up;
        m_Animator.applyRootMotion = false;
    }
}

2. 地面状态下强制重置Jump参数

修改UpdateAnimator方法,当角色处于地面时,主动将Jump参数设为0:

void UpdateAnimator(Vector3 move)
{
    // 更新基础动画参数
    m_Animator.SetFloat("Forward", m_ForwardAmount, 0.1f, Time.deltaTime);
    m_Animator.SetFloat("Turn", m_TurnAmount, 0.1f, Time.deltaTime);
    m_Animator.SetBool("Crouch", m_Crouching);
    m_Animator.SetBool("OnGround", m_IsGrounded);
    
    // 新增:地面状态下重置Jump参数为0
    if (m_IsGrounded)
    {
        m_Animator.SetFloat("Jump", 0f);
    }
    else
    {
        m_Animator.SetFloat("Jump", m_Rigidbody.velocity.y);
    }

    // 计算跳跃动画的腿部偏移逻辑
    float runCycle =
        Mathf.Repeat(
            m_Animator.GetCurrentAnimatorStateInfo(0).normalizedTime + m_RunCycleLegOffset, 1);
    float jumpLeg = (runCycle < k_Half ? 1 : -1) * m_ForwardAmount;
    if (m_IsGrounded)
    {
        m_Animator.SetFloat("JumpLeg", jumpLeg);
    }

    // 动画速度控制
    if (m_IsGrounded && move.magnitude > 0)
    {
        m_Animator.speed = m_AnimSpeedMultiplier;
    }
    else
    {
        m_Animator.speed = 1;
    }
}

3. 额外排查点

  • 确认角色Rigidbody的Use Gravity处于勾选状态,且Constraints仅冻结旋转(现有代码已配置)。
  • 检查地面对象是否有有效Collider组件,且其Layer未被射线检测排除。
  • 若角色模型较高,可适当调大m_GroundCheckDistance参数(比如设为0.2f),确保射线能触达地面。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 00:35:53