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

Unity3D CharacterController跳跃落平台弹跳异常问题求助

修复CharacterController角色落地弹跳问题

问题原因

你遇到的弹跳现象,核心是CharacterController的isGrounded检测存在帧延迟,加上当前重力值设置过小(-1f),导致跳跃落地时角色垂直速度没被及时清零,触发了微小的碰撞反弹。而高处掉落时垂直速度足够大,isGrounded检测更准确,所以无此问题。

修复方案

1. 调整重力值

当前重力值太小,角色下落速度过慢,容易导致isGrounded误判。改成接近真实重力的值:

[SerializeField] private float gravityValue = -9.81f;

2. 优化落地速度处理

落地时给垂直速度设一个微小负值,让角色稳定贴地,避免碰撞反弹:

if (groundedPlayer && playerVelocity.y < 0)
{
    playerVelocity.y = -0.5f; // 替代原来的0,确保贴合地面
    _animator.SetBool("Jumping", false);
}

3. 修改CharacterController组件参数

在Inspector面板调整:

  • Skin Width:设为0.01-0.05(默认0.08过大,易引发检测误差)
  • Min Move Distance:设为0,避免微小移动被忽略导致弹跳

4. 修正跳跃速度计算

把跳跃速度的计算系数从-3.0f改成-2.0f,符合自由落体物理公式,让速度变化更自然:

playerVelocity.y += Mathf.Sqrt(jumpHeight * -2.0f * gravityValue);

修改后的核心代码片段

[SerializeField] private float gravityValue = -9.81f;

private void MovePlayer()
{
    float x = Input.GetAxis("Horizontal");
    groundedPlayer = controller.isGrounded;
    
    if (groundedPlayer && playerVelocity.y < 0)
    {
        playerVelocity.y = -0.5f;
        _animator.SetBool("Jumping", false);
    }

    Vector3 move = new Vector3(x, 0,0);
    controller.Move(move * Time.deltaTime * playerSpeed);

    if (move != Vector3.zero)
    {
        gameObject.transform.forward = move;
    }

    if (Input.GetButtonDown("Jump") && groundedPlayer)
    {
        _animator.SetBool("Jumping", true);
        playerVelocity.y += Mathf.Sqrt(jumpHeight * -2.0f * gravityValue);
    }

    playerVelocity.y += gravityValue * Time.deltaTime;
    controller.Move(playerVelocity * Time.deltaTime);

    if (groundedPlayer)
    {
        _animator.SetInteger("Speed", Convert.ToInt32(x));
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 03:20:52