You need to enable JavaScript to run this app. Godot跳跃动画输入触发后存在轻微延迟且无法完整播放问题咨询
优惠活动
大模型
产品
解决方案
定价
更多

Godot跳跃动画输入触发后存在轻微延迟且无法完整播放问题咨询

项目背景

这是我最初的几个项目之一,我跟随YouTube上的教程编写了以下代码。就我目前看到的内容而言,该教程没有涉及动画相关的实现,因此我尝试自行完成相关功能,目前 idle(待机)和 run(奔跑)动画均可以正常运行。跳跃功能本身可以正常触发,但跳跃动画会在输入后延迟一小段时间才启动,同时由于角色落地过快,导致动画无法完成完整循环(最后这个问题我会尝试通过调整动画播放速度解决)

原始代码
extends Actor

func _physics_process(delta):
    var direction = get_direction()
    velocity = calculate_move_velocity(velocity, direction, maxSpeed)
    velocity = move_and_slide(velocity, FLOOR_NORMAL) # 角色移动核心方法

func get_direction() -> Vector2:
    return Vector2(
        Input.get_action_strength("right") - Input.get_action_strength("left"),
        -1.0 if Input.is_action_just_pressed("jump") and is_on_floor() else 0.0
    )

func calculate_move_velocity(               # 移动与动画逻辑
        linear_velocity: Vector2,
        direction: Vector2,
        maxSpeed: Vector2
    ) -> Vector2:
    var new_velocity = linear_velocity      # 存储角色线性移动速度
    new_velocity.x = maxSpeed.x * direction.x
    new_velocity.x = lerp(new_velocity.x, 0, 0.1)

    # 向右移动逻辑
    if is_on_floor() and direction.x == 1.0:                  
        $AnimationPlayer.play("run")
        $Sprite.scale.x = 1
    # 向左移动逻辑
    elif is_on_floor() and direction.x == -1.0:               
        $AnimationPlayer.play("run")
        $Sprite.scale.x = -1
    # 待机逻辑
    if is_on_floor() and direction.x == 0.0:                  
        $AnimationPlayer.play("idle")
    
    new_velocity.y += gravity * get_physics_process_delta_time()

    # 跳跃逻辑
    if direction.y == -1.0:                 
        new_velocity.y = maxSpeed.y * direction.y
    
    # 跳跃动画触发逻辑
    if !is_on_floor() == false and Input.is_action_just_pressed("jump"):
        $AnimationPlayer.play("jump")
    
    return new_velocity 
问题原因
  • 跳跃动画触发条件逻辑错误:!is_on_floor() == false 等价于判断is_on_floor() == true,也就是角色还在地面时才触发跳跃动画,同时该判断在速度赋值之后,此时角色已经开始起跳,状态判断出现偏差,导致动画延迟
  • 地面动画每帧重复调用play()方法,会覆盖刚触发的跳跃动画:只要角色还在地面,每帧都会重新播放run/idle动画,导致跳跃动画被顶掉,需要等地面动画的第一帧播放完成后才会切换到跳跃动画,产生延迟感
  • 跳跃动画没有优先级设置,角色落地后会立刻切换到idle/run动画,导致跳跃动画还没播放完就被中断
修复方案
  1. 调整跳跃动画的触发时机,放在地面动画判断之前,并且修正条件逻辑
  2. 新增动画播放状态判断,只有当前没有播放跳跃动画、且角色在地面时,才切换地面动画
  3. 可以通过监听动画完成事件,避免跳跃动画被提前中断

修复后代码

extends Actor

func _physics_process(delta):
    var direction = get_direction()
    velocity = calculate_move_velocity(velocity, direction, maxSpeed)
    velocity = move_and_slide(velocity, FLOOR_NORMAL) # 角色移动核心方法

func get_direction() -> Vector2:
    return Vector2(
        Input.get_action_strength("right") - Input.get_action_strength("left"),
        -1.0 if Input.is_action_just_pressed("jump") and is_on_floor() else 0.0
    )

func calculate_move_velocity(               # 移动与动画逻辑
        linear_velocity: Vector2,
        direction: Vector2,
        maxSpeed: Vector2
    ) -> Vector2:
    var new_velocity = linear_velocity      # 存储角色线性移动速度
    new_velocity.x = maxSpeed.x * direction.x
    new_velocity.x = lerp(new_velocity.x, 0, 0.1)

    # 优先处理跳跃动画触发
    if Input.is_action_just_pressed("jump") and is_on_floor():
        $AnimationPlayer.play("jump")

    # 只有当前不在播放跳跃动画、且角色在地面时,才切换地面动画
    if is_on_floor() and $AnimationPlayer.current_animation != "jump":
        if direction.x == 1.0:                  
            $AnimationPlayer.play("run")
            $Sprite.scale.x = 1
        elif direction.x == -1.0:               
            $AnimationPlayer.play("run")
            $Sprite.scale.x = -1
        elif direction.x == 0.0:                  
            $AnimationPlayer.play("idle")
    
    new_velocity.y += gravity * get_physics_process_delta_time()

    # 跳跃逻辑
    if direction.y == -1.0:                 
        new_velocity.y = maxSpeed.y * direction.y
    
    return new_velocity 
额外优化建议

如果还是存在跳跃动画播放不全的问题,可以调整两个参数:

  • 降低gravity数值,减慢角色下落速度,延长空中停留时间
  • 在AnimationPlayer中给跳跃动画设置loop = false,并且监听animation_finished信号,动画播放完成后再允许切换到地面动画

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.18 11:24:03