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

Configurable Joint的yDrive模式问题——Brackeys多人FPS教程相关

解决Brackeys多人FPS教程中跳跃按钮无效的问题

问题分析

你碰到的跳跃完全无效的问题,核心原因是ConfigurableJoint的驱动设置没正确覆盖Unity默认配置,或者PlayerMotor里的推力应用逻辑有疏漏。结合你的代码,主要有几个可修复的点:

修复步骤

  1. 调整ConfigurableJoint的驱动模式切换逻辑
    原来的代码在跳跃时只把弹簧设为0,但JointDriveMode.Position模式下即使弹簧为0,关节还是会有位置约束。建议跳跃时直接把驱动模式切为JointDriveMode.None,落地后再切回Position:

    private void SetJointSettings(float _jointSpring)
    {
        joint.yDrive = new JointDrive
        {
            // 弹簧为0时直接禁用驱动,让角色能正常跳跃
            mode = _jointSpring == 0 ? JointDriveMode.None : jointMode,
            positionSpring = _jointSpring, 
            maximumForce = joinMaxForce 
        };
    }
    
  2. 检查ConfigurableJoint的组件基础配置

    • 确认Connected Body正确关联到角色的胶囊体Rigidbody,而不是空对象
    • 保证Axis设置为Y轴向上,Secondary Axis为X轴
    • 关闭Auto Configure Connected Anchor,手动将锚点拖到角色重心位置
  3. 验证PlayerMotor的ApplyThruster方法
    你的PlayerMotor类里的ApplyThruster必须正确把力加到Rigidbody上,示例实现如下:

    public class PlayerMotor : MonoBehaviour
    {
        private Rigidbody rb;
    
        void Start()
        {
            rb = GetComponent<Rigidbody>();
        }
    
        // 其他方法...
    
        public void ApplyThruster(Vector3 force)
        {
            if (rb != null)
            {
                rb.AddForce(force, ForceMode.Force);
            }
        }
    }
    

    注意要确保角色的Rigidbody没有勾选Kinematic,否则加力不会生效。

  4. 增大推力数值
    代码里的thrusterForce = 1000f可能太小,根据角色Rigidbody的Mass调整,比如改成5000f试试(如果角色Mass是1的话,这个数值足够让角色跳起来)。

修正后的完整PlayerController代码

using UnityEngine;
[RequireComponent(typeof(PlayerMotor))]
[RequireComponent(typeof(ConfigurableJoint))]

public class PlayerController : MonoBehaviour
{
    private Vector3 _movHorizontal;
    private Vector3 _movVertical;
    private Vector3 _velocity;
    
    [SerializeField]
    private float speed = 5f;
    [SerializeField]
    private float lookSensitivity = 5f;
    [SerializeField]
    private float thrusterForce = 5000f;

    [Header("Spring Settings:")]
    [SerializeField]
    private JointDriveMode jointMode = JointDriveMode.Position;
    [SerializeField]
    private float jointSpring = 20f;
    [SerializeField]
    private float joinMaxForce = 40f;

    private PlayerMotor motor;
    private ConfigurableJoint joint;

    void Start ()
    {
        motor = GetComponent<PlayerMotor>();
        joint = GetComponent<ConfigurableJoint>();
        SetJointSettings(jointSpring);
    }

    void Update () 
    {
        float _xMov = Input.GetAxisRaw("Horizontal");
        float _zMov = Input.GetAxisRaw("Vertical");

        _movHorizontal = transform.right * _xMov;
        _movVertical = transform.forward * _zMov;
        _velocity = (_movHorizontal + _movVertical).normalized * speed;
        motor.Move(_velocity); 

        float _yrot = Input.GetAxisRaw("Mouse X");
        Vector3 _rotation = new Vector3 (0f, _yrot, 0f) * lookSensitivity;
        motor.Rotate(_rotation);

        float _xrot = Input.GetAxisRaw("Mouse Y");
        Vector3 _cameraRotation = new Vector3 (_xrot, 0f, 0f) * lookSensitivity;
        motor.RotateCamera(_cameraRotation);

        Vector3 _thrusterForce = Vector3.zero;
        if (Input.GetButton("Jump"))
        {
            _thrusterForce = Vector3.up * thrusterForce;
            SetJointSettings(0f);
        } else 
        {
            SetJointSettings(jointSpring);
        }
        motor.ApplyThruster(_thrusterForce);
    }

    private void SetJointSettings(float _jointSpring)
    {
        joint.yDrive = new JointDrive
        {
            mode = _jointSpring == 0 ? JointDriveMode.None : jointMode,
            positionSpring = _jointSpring, 
            maximumForce = joinMaxForce 
        };
    }
}

额外检查项

  • 确认输入设置里的“Jump”按钮绑定正确(默认是空格键)
  • 检查角色和地面都有Collider组件,且地面的Collider未勾选Is Trigger(保证重力生效)
  • 查看Unity控制台是否有隐藏警告(比如Rigidbody未找到、Joint连接错误这类信息)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 00:41:38