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

求助:Godot角色攻击动画结束后冻结,无法切换至Idle或行走状态

角色攻击后冻结问题的修复方案

问题根源与修复步骤

  • 攻击状态未重置:你通过is_attacking = true标记攻击状态,但攻击动画结束后没有触发状态重置,导致后续移动、动画切换的逻辑都被!is_attacking的判断拦截。
    修复:给攻击动画绑定结束信号,调用set_attacking_false()。

    • 打开AnimationTree的攻击动画轨道,找到finished信号,连接到角色脚本的set_attacking_false()方法;
    • 或者在攻击动画资源里添加Call Method轨道,结尾调用角色节点的set_attacking_false()函数。
  • 动画树路径空格错误:脚本里的parameters/movements /transition_request存在多余空格,正确路径应为parameters/movements/transition_request,否则动画树无法识别切换指令。

  • 变量拼写错误:animemation_tree多写了一个m,统一改成animation_tree,避免动画混合逻辑失效。

修复后的完整脚本

const SPEED = 5.0
const JUMP_VELOCITY = 4.5

var is_attacking = false

@onready var animation_tree: AnimationTree = $animation_tree
var smooth_animation := Vector2()

func set_attacking_false():
    is_attacking = false
    
func _physics_process(delta: float) -> void:
    # 添加重力
    if not is_on_floor():
        velocity += get_gravity() * delta
        animation_tree.set("parameters/in_air/transition_request", "true")
        
    # 处理跳跃
    if Input.is_action_just_pressed("ui_accept") and is_on_floor():
        velocity.y = JUMP_VELOCITY
        
    if Input.is_action_just_pressed("mouse_left") and is_on_floor():
        is_attacking = true
        animation_tree.set("parameters/movements/transition_request", "attack")
    
    
    # 获取输入方向并处理移动/减速
    # 最佳实践:应使用自定义游戏玩法动作替换UI动作
    var horizontal_rotation = $camera/horizontal.global_transform.basis.get_euler().y
    var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
    var direction := (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized().rotated(Vector3.UP, horizontal_rotation)
    if direction and !is_attacking:
        velocity.x = direction.x * SPEED
        velocity.z = direction.z * SPEED
        $mesh.rotation.y = lerp_angle($mesh.rotation.y, atan2(-direction.x, -direction.z), delta * 5)
        
    else:
        velocity.x = move_toward(velocity.x, 0, SPEED)
        velocity.z = move_toward(velocity.z, 0, SPEED)
    if is_on_floor():
        animation_tree.set("parameters/in_air/transition_request", "false")
        if !is_attacking:
            if (input_dir.x != 0 or input_dir.y != 0):
                animation_tree.set("parameters/movements/transition_request", "walk")
                
            else:
                animation_tree.set("parameters/movements/transition_request", "idle")
    
    
    smooth_animation = lerp(smooth_animation, input_dir, delta * 10) 
    animation_tree.set("parameters/blend_position", smooth_animation.length())
    move_and_slide()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 09:26:21