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

玩家离开梯子后攀爬动画未及时切换的问题排查求助

玩家离开梯子后攀爬动画未及时切换的问题排查求助

大家好,我在开发Unity 2D游戏的梯子攀爬功能时遇到了一个动画延迟的问题:当玩家触碰到梯子并按下上键时,攀爬动画能正常触发;但如果此时按左右方向键让玩家离开梯子区域,明明玩家已经不在梯子上了,攀爬动画却还会卡顿1-2秒才切换回奔跑或 idle 状态,视觉上很不流畅。我录了操作过程的视频,能清晰看到多次离开梯子后攀爬动画残留的情况。

以下是我的PlayerMovement脚本代码:

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public CharacterController2D controller;
    float horizontalMove = 0f;
    float verticalMove = 0f;
    public float runSpeed = 40f;
    bool jump = false;
    public Animator animator;
    private bool isLadder = false;
    public Rigidbody2D rigidBody2D;
    Collider2D tempCollision;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        horizontalMove = Input.GetAxisRaw("Horizontal") * runSpeed;
        verticalMove = Input.GetAxisRaw("Vertical") * runSpeed;

        animator.SetFloat("Speed", Mathf.Abs(horizontalMove));
        //Debug.Log("isLadder:" + isLadder);

        if(Input.GetButtonDown("Left") || Input.GetButtonDown("Right"))
        {
            //Debug.Log("Left or Right");
            animator.SetBool("Climb", false);
        }

        if (Input.GetButtonDown("Up") && isLadder)
        {
            //Debug.Log("Up");
            animator.SetBool("Climb", true);
        }

        if (Input.GetButtonDown("Jump") && !isLadder)
        {
            jump = true;
        }
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
        //Debug.Log("Object that collided with me: " + collision.gameObject.tag);
        if (collision.gameObject.tag == "Ladder" || collision.gameObject.tag == "Ladder_Bottom")
        {
            tempCollision = collision;
            //Debug.Log(collision.gameObject.name);
            isLadder = true;
        }
    }

    void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Ladder" || collision.gameObject.tag == "Ladder_Bottom")
        {
            tempCollision = null;
            //Debug.Log(collision.gameObject.name);
            animator.SetBool("Climb", false);
            isLadder = false;
        }
    }

    public void OnLanding()
    {
        animator.SetBool("IsJumping", false);
    }

    private void FixedUpdate()
    {
        controller.Move(horizontalMove * Time.fixedDeltaTime, false, jump);
        jump = false;
        //isLadder = false;
    }
}

我已经尝试在检测到玩家按下左右键时,直接将动画控制器的Climb布尔值设为false,但这个方法并没有完全解决问题,动画延迟的情况依然存在。想请教各位大佬,我的代码哪里存在逻辑漏洞?应该怎么调整才能让动画在玩家离开梯子时立刻切换?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.07 10:18:06