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

Unity中使用bool判断玩家跳跃状态触发动画失败求助

解决你的2D角色跳跃动画不播放问题

嘿,我看到你在刚接触编程开发游戏动画系统时遇到了跳跃动画不触发的问题,咱们一步步拆解问题,然后给出修复方案:

问题根源分析

1. isJumping 变量被错误地立即重置

在你的Character2dController脚本的Update函数末尾,你写了isJumping = false;——这意味着即使Jump()函数里刚把isJumping设为true,紧接着这行代码就会把它改回false,导致这个变量几乎不会保持true的状态,动画自然无法触发。

2. 动画脚本没有实时同步控制器的isJumping状态

在CharacterAnimation的Start()函数里,你只读取了一次Character2dController的isJumping初始值,之后再也没有同步过最新状态;而且你还手动加了isJumping = false;,直接覆盖了初始读取的值,所以哪怕控制器里的变量变化了,动画脚本里的isJumping也永远是false。


修复后的代码示例

第一步:修复Character2dController脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Character2dController : MonoBehaviour {
    public float MovementSpeed = 8;
    public float JumpForce = 5;
    public Transform feet;
    public bool isJumping;
    private Rigidbody2D rb;
    public LayerMask groundLayers;

    void Start() {
        rb = GetComponent<Rigidbody2D>();
        isJumping = false;
    }

    void Update() {
        Jump();
        // 左右移动逻辑
        var movement = Input.GetAxis("Horizontal");
        transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * MovementSpeed;
        
        // 只有落地时才重置isJumping,而不是每帧都重置
        if (IsGrounded()) {
            isJumping = false;
        }
    }

    void Jump() {
        if (Input.GetButtonDown("Jump") && IsGrounded()) {
            isJumping = true;
            rb.AddForce(new Vector2(0, JumpForce), ForceMode2D.Impulse);
        }
    }

    public bool IsGrounded() {
        // 简化地面检测的判断逻辑
        Collider2D groundCheck = Physics2D.OverlapCircle(feet.position, 0.5f, groundLayers);
        return groundCheck != null;
    }
}

第二步:修复CharacterAnimation脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterAnimation : MonoBehaviour {
    private Animator anim;
    private Character2dController controller; // 保存控制器的引用,用于实时读取状态
    private Rigidbody2D rb;

    void Start() {
        anim = GetComponent<Animator>();
        controller = GetComponent<Character2dController>();
        rb = GetComponent<Rigidbody2D>();
    }

    void Update() {
        // 行走动画:使用Horizontal轴输入更准确,避免单独判断按键
        float horizontalInput = Input.GetAxis("Horizontal");
        anim.SetBool("isWalking", Mathf.Abs(horizontalInput) > 0.1f);

        // 角色翻转:同样用轴输入判断更流畅
        if (horizontalInput < -0.1f) {
            transform.localScale = new Vector3(-1, 1, 1);
        } else if (horizontalInput > 0.1f) {
            transform.localScale = new Vector3(1, 1, 1);
        }

        // 跳跃动画:实时读取控制器的isJumping状态
        if (controller.isJumping) {
            anim.SetTrigger("Up");
            // 可选:触发一次后重置控制器的isJumping,避免重复触发动画
            controller.isJumping = false;
        }

        // 【进阶方案】更可靠的跳跃判断(不依赖isJumping变量):
        // if (!controller.IsGrounded() && rb.velocity.y > 0) {
        //     anim.SetTrigger("Up");
        // }
    }
}

额外建议

对于跳跃动画,其实可以不依赖isJumping变量,直接通过角色是否接地和Rigidbody的Y轴速度来判断状态:

  • 当角色不在地面且Y速度>0:播放跳跃上升动画
  • 当角色不在地面且Y速度<0:播放下落动画
  • 当角色落地:播放落地动画

这种方式避免了变量同步的问题,状态判断也更准确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 08:06:44