Unity新输入系统异常:特定操作下角色无法跳跃
Unity新输入系统角色跳跃异常问题
问题现象
- 角色按W+A行走、或W/W+A/W+D奔跑时,无法触发跳跃,
SetJump方法内的Debug日志无输出 - 其他移动场景下跳跃功能正常
- 已确认
IsGrounded方法功能正常,角色处于平坦地形 - 角色已正确挂载CharacterController、Capsule Collider、Player Input组件,方法绑定无误
输入系统配置
- [输入系统配置截图1]
- [输入系统配置截图2]
- [输入系统配置截图3]
- [输入系统配置截图4]
完整代码
public class PlayerMovement : MonoBehaviour { [Header("玩家配置")] [SerializeField] private float speed = 3f; [SerializeField] private float speedWhileRunningMultiplier = 1.3f; [SerializeField] private float gravity = -9.8f; [SerializeField] private float gravityMultiplier = 3f; [SerializeField] private float jumpForce = 9.8f; [SerializeField] private float smoothTime = 0.05f; [SerializeField] private bool isGrounded = false; private CharacterController controller; private Animator anim; private bool isWalking, isRunning = false;// 用于动画控制 private float currentSpeedRotating; private float verticalVelocity = 0f; private Vector2 movement2D; private Vector3 direction; private bool IsGrounded() => controller.isGrounded; private void Awake() { controller = GetComponent<CharacterController>(); anim = GetComponent<Animator>(); } void FixedUpdate() { ApplyMovement(); ApplyRotation(); ApplyGravity(); } public void SetMovement(InputAction.CallbackContext value) { movement2D = value.ReadValue<Vector2>(); direction = new Vector3(movement2D.x, 0, movement2D.y); direction.Normalize(); } public void SetJump(InputAction.CallbackContext value) { if (value.started) Debug.Log("准备跳跃"); if (!value.started) return; if (!IsGrounded()) return; verticalVelocity += jumpForce; } public void SetRun(InputAction.CallbackContext value) { isRunning = value.performed ? true : false; } void ApplyMovement() { isWalking = direction.x != 0 || direction.z != 0; float currentSpeed = speed * (isRunning ? speedWhileRunningMultiplier : 1f); controller.Move(direction * currentSpeed * Time.deltaTime); anim.SetBool("isWalking", isWalking); anim.SetBool("isRunning", isRunning); } void ApplyRotation() { if (movement2D.sqrMagnitude == 0) return; float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg; float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref currentSpeedRotating, smoothTime); transform.rotation = Quaternion.Euler(0.0f, angle, 0.0f); } void ApplyGravity() { if (IsGrounded() && verticalVelocity <0.0f) { verticalVelocity = -1f; } else { verticalVelocity += gravity * gravityMultiplier * Time.deltaTime; } direction.y = verticalVelocity; } }
排查与解决方案
1. 输入动作优先级/交互冲突
这是最可能的原因,因为仅在多按键组合时失效:
- 打开输入系统配置,检查
Run动作的Interaction设置,若为Hold类交互,确认是否与Jump动作的输入触发逻辑互斥 - 确保
Jump动作的交互类型为Press,且未设置任何会拦截输入的规则 - 检查动作的
Canceled触发条件,避免多按键组合时跳跃动作被意外取消
2. 输入绑定冲突检查
- 确认跳跃按键(如Space)未被
Movement或Run动作重复绑定 - 临时将跳跃绑定到其他按键(如K),测试相同移动场景下是否能触发,排除原按键的硬件/系统级冲突
3. 代码临时验证
若怀疑是输入触发问题,可临时修改SetJump方法,移除地面判断,验证Debug日志是否输出:
public void SetJump(InputAction.CallbackContext value) { if (value.started) Debug.Log("准备跳跃"); if (!value.started) return; // 临时注释地面判断,测试输入是否触发 // if (!IsGrounded()) return; verticalVelocity += jumpForce; }
4. Player Input组件设置
- 确认Player Input的
Behavior为Invoke Unity Events,且Jump动作与SetJump方法绑定无误 - 检查运行时
Action Map是否处于激活状态,无其他逻辑切换到无效Map
内容的提问来源于stack exchange,提问作者Jamelaumn
相关产品推荐
相关产品推荐

