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

Unity 3D玩家移动正常但跳跃绑定失效问题求助

Unity角色跳跃故障排查与修复方案

问题根源分析

你的代码存在几个关键问题,直接导致跳跃失效:

1. 组件逻辑冲突

同时混用CharacterController和Rigidbody的跳跃逻辑:

  • 代码中同时存在给Rigidbody加力的代码,以及CharacterController的velocity跳跃逻辑
  • 这两个组件的移动体系完全互斥,CharacterController会忽略Rigidbody的物理效果,反过来Rigidbody也会干扰CharacterController的地面检测与移动计算

2. 输入检测顺序错误

先执行了一次Jump输入检测并触发无效逻辑,消耗了Input.GetButtonDown的单次触发状态,导致后续基于CharacterController的跳跃判断无法触发。

3. 潜在设置问题

  • groundMask可能未正确选中地面图层
  • groundCheck的位置可能不在角色底部,导致地面检测失效

修复后的完整代码

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public CharacterController controller;

    public float speed = 12f;
    public float gravity = -9.81f * 2;
    public float jumpHeight = 3f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity;
    bool isGrounded;

    void Update()
    {
        // 优先执行地面检测
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        // 接触地面时重置下落速度,避免下次下落过快
        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }

        // 水平移动处理
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
        Vector3 move = transform.right * x + transform.forward * z;
        controller.Move(move * speed * Time.deltaTime);

        // 跳跃逻辑(仅保留CharacterController方案)
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

        // 应用重力
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
    }
}

额外检查项

  1. 删除角色对象上的Rigidbody组件,彻底避免逻辑冲突
  2. 确认groundMask仅选中地面所在的图层,不要包含玩家自身图层
  3. 调整groundCheck的Transform位置到角色模型底部,确保检测范围能准确触地
  4. 检查Input Settings中Jump按钮的绑定(默认空格键无需修改)
  5. 确认CharacterController的Slope Limit、Step Offset参数设置合理,避免角色卡地面

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.01 13:14:58