2D角色跳跃动画落地后卡在循环问题求助
2D角色跳跃动画落地后循环卡顿问题
问题:给2D玩家角色添加了跳跃动画,起跳时动画正常播放,但落地后跳跃动画陷入循环卡顿,查看Animator面板发现跳跃动画始终处于循环播放状态。
现有代码
角色移动与跳跃控制代码:
void Update() { horizontal = Input.GetAxisRaw("Horizontal"); animator.SetFloat("Speed", Mathf.Abs(horizontal)); if (Input.GetButtonDown("Jump") && IsGrounded()) { rb.velocity = new Vector2(rb.velocity.x, jumpingPower); jump = true; animator.SetBool("isJumping", true); } if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f) { rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f); } if (Input.GetButtonUp("Jump") && IsGrounded()) { animator.SetBool("isJumping", false); animator.SetBool("isGrounded", true); jump = false; } Flip(); }
落地判断代码:
private bool IsGrounded() { return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer); }
问题原因
当前代码仅在松开跳跃键且角色落地时才会将isJumping设为false,但实际场景中,角色落地时玩家可能已经松开了跳跃键(此时Input.GetButtonUp("Jump")已触发过,不会再进入该分支),或者落地时玩家还按着跳跃键,这两种情况都会导致isJumping一直保持true,跳跃动画持续循环。
解决方案
修改逻辑,将落地状态检测与跳跃键输入解绑,每帧单独判断角色是否落地,只要落地就重置跳跃动画状态:
void Update() { horizontal = Input.GetAxisRaw("Horizontal"); animator.SetFloat("Speed", Mathf.Abs(horizontal)); if (Input.GetButtonDown("Jump") && IsGrounded()) { rb.velocity = new Vector2(rb.velocity.x, jumpingPower); jump = true; animator.SetBool("isJumping", true); animator.SetBool("isGrounded", false); // 起跳时标记离开地面 } if (Input.GetButtonUp("Jump") && rb.velocity.y > 0f) { rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f); } // 独立处理落地逻辑,每帧检测 bool isGrounded = IsGrounded(); if (isGrounded) { if (jump) { animator.SetBool("isJumping", false); animator.SetBool("isGrounded", true); jump = false; } } else { animator.SetBool("isGrounded", false); } Flip(); }
额外检查项:
- 打开跳跃动画剪辑的Inspector面板,确认取消勾选
Loop Time选项,避免动画本身强制循环 - 检查Animator控制器的状态切换条件:确保从跳跃状态切换回 idle/run 状态的条件是
isJumping == false且isGrounded == true
内容的提问来源于stack exchange,提问作者Toasty
相关产品推荐
相关产品推荐

