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

Unity角色设置crouchSpeed低于movementSpeed但蹲伏时移动速度反而更快问题

问题原因排查
  • 蹲伏状态的移动计算逻辑执行后,被后续全局移动逻辑重复叠加了速度系数
    你提供的代码仅展示了蹲伏状态下的速度计算片段,大概率你在isCrouching判断块之外,还存在全局的移动速度计算逻辑(比如统一对movement乘以movementSpeed或者Time.deltaTime)。你在蹲伏逻辑里已经给水平速度乘了crouchSpeed,如果后续再被全局乘一次movementSpeed(10.0f),最终蹲伏速度就会变成2*10=20,远高于正常行走的10。
  • 代码存在无效逻辑和拼写错误
    你在蹲伏块中写了movement *= crouchingMulitplier,但后续立刻用transform.forward和transform.right的计算结果覆盖了movement的水平值,这行乘以系数的代码完全没有生效。另外crouchingMulitplier存在拼写错误,正确拼写为crouchingMultiplier,拼写错误会导致变量未初始化或调用失败。
  • 缺少蹲伏状态退出逻辑
    当前代码只有按下左CTRL将isCrouching设为true的逻辑,没有松开CTRL时将isCrouching重置为false的逻辑,会导致按一次CTRL后角色永久保持蹲伏状态。
  • 速度计算缺失时间系数
    如果你正常行走的速度计算中乘以了Time.deltaTime来保证帧率无关,但蹲伏逻辑里的curSpeedX、curSpeedY计算没有乘Time.deltaTime,也会导致蹲伏速度远高于正常行走速度。
修复方案
  1. 首先补充蹲伏退出逻辑:
if (Input.GetKey(KeyCode.LeftControl))
{
    isCrouching = true;
}
// 新增松开CTRL退出蹲伏的逻辑
else if (Input.GetKeyUp(KeyCode.LeftControl))
{
    isCrouching = false;
    // 这里记得把controller.height改回正常站立高度
    controller.height = standingHeight;
}
  1. 调整蹲伏逻辑的速度计算顺序,移除无效代码,统一时间系数:
if (isCrouching == true)
{
    controller.height = crouchingHeight;
    float currentSpeed = canMove ? crouchSpeed : 0;
    float curSpeedX = currentSpeed * Input.GetAxis("Vertical");
    float curSpeedY = currentSpeed * Input.GetAxis("Horizontal");
    // 保留重力的Y轴数值
    float movementY = movement.y;
    movement = (transform.forward * curSpeedX) + (transform.right * curSpeedY);
    movement.y = movementY;
    // 统一和正常行走逻辑一致,乘以Time.deltaTime
    movement *= Time.deltaTime;
}
  1. 检查全局逻辑,不要对已经在状态分支里计算过速度的movement变量重复乘以movementSpeed,避免速度叠加。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.15 08:15:00