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

求助:Godot 4中攻击动画被其他动画覆盖的问题

Godot角色剑攻击动画被覆盖的问题修复

问题根源

  1. _physics_process开头每帧强制重置attacking = false,导致攻击状态无法持续维持
  2. 攻击输入仅在按键按下的单帧生效,后续帧直接将attacking设为false,动画仅能播放第一帧
  3. 部分非攻击动画的播放逻辑未完全排除攻击状态,导致攻击动画被中途打断

修复方案

  1. 移除_physics_process开头的attacking = false,保留攻击状态的持续性
  2. 重构攻击触发逻辑:仅在未攻击时按下按键才启动攻击,通过动画结束信号重置攻击状态
  3. 给所有非攻击动画的播放条件添加and not attacking判断,确保攻击过程不被打断

修改后的完整代码

extends CharacterBody2D

const normalspeed = 400.0
const JUMP_VELOCITY = -600.0
var jump_count = 0
var dashing = false
var attacking = false  # 初始状态保持false,不再每帧强制重置
var dash_count = 0
var stamina = 100
var game_running = true
var totaldashcount = 0
var player_moving = false
var frontflipping = false
var health = 100

const cooldown=preload("res://Scripts/Cooldown.gd")

@onready var dash_cooldown=cooldown.new(2)

const dashspeed = 2500
const dashlength = 0.15

@onready var dash = $Dash

var gravity = ProjectSettings.get_setting("physics/2d/default_gravity") * 1.7

@onready var anim = get_node("AnimationPlayer")


func _physics_process(delta):
    
    player_moving = false
    dashing = false  # 仅重置这两个状态,保留attacking的持续性
    
    $/root/World/UI.update_stamina()
    $/root/World/UI.stamina_value(stamina)
    if stamina != 0.9:
        $/root/World/UI.stamina_value(stamina)
    
    dash_cooldown.tick(delta)
    
    if Input.is_action_just_pressed("Dash") and stamina >= 100:
        if dash_count == 0:
            if velocity.x != 0:
                if not attacking:
                    anim.play("Dash")
                dashing = true
                dash_count += 1
                totaldashcount += 1
                player_moving = true
            dash.start_dash(dashlength)
            dashing = true
            if velocity.x != 0:
                if not attacking:
                    anim.play("Dash")
                dashing = true
                dash_count += 1
                totaldashcount += 1
                player_moving = true
            $/root/World/UI.update_stamina()
            stamina = 0     
    
    var speed = dashspeed if dash.is_dashing() else normalspeed
    
    if velocity.x != 0:
        player_moving = true
    else:
        player_moving = false
    
    # 重力处理
    if not is_on_floor():
        velocity.y += gravity * delta
    elif is_on_floor():
        velocity.y = 0
        jump_count = 0
        dash_count = 0
        
    if Input.is_action_just_pressed("ui_up") and is_on_floor() or Input.is_action_just_pressed("w") and is_on_floor():
        velocity.y = JUMP_VELOCITY
        if not attacking:
            anim.play("Jump")
    
    var direction_letter = Input.get_axis("a", "d")
    var direction_arrow = Input.get_axis("ui_left", "ui_right")
    
    if direction_letter == -1:
        get_node("AnimatedSprite2D").flip_h = true
    elif direction_letter == 1:
        get_node("AnimatedSprite2D").flip_h = false
    if direction_arrow == -1:
        get_node("AnimatedSprite2D").flip_h = true
    elif direction_arrow == 1:
        get_node("AnimatedSprite2D").flip_h = false
        
    # WASD移动
    if direction_letter:
        velocity.x = direction_letter * speed
        if velocity.y == 0 and not dash.is_dashing() and velocity.x != 0 and not attacking:
            anim.play("Walking")
    
    # 方向键移动
    elif direction_arrow:
        velocity.x = direction_arrow * speed
        if velocity.y == 0 and not dash.is_dashing() and velocity.x != 0 and not attacking:
            anim.play("Walking")
    else:
        velocity.x = move_toward(velocity.x, 0, speed)
        if velocity.y == 0 and not attacking:
            anim.play("Idle")
            
    if velocity.y > 400 and dashing == true:
        if not frontflipping and not is_on_floor() and not attacking:
            anim.play("Fall")
    elif velocity.y > 100 and dashing == false:
        if not frontflipping and not is_on_floor() and not attacking:
            anim.play("Fall")       
    
    if velocity.y != 0 and (Input.is_action_just_pressed("ui_up") or Input.is_action_just_pressed("w")):
        if jump_count == 0 or jump_count == 1:
            jump_count += 1
            velocity.y = JUMP_VELOCITY
            if jump_count == 2 and not attacking:
                anim.play("Frontflip")
                frontflipping = true

    # 重构攻击逻辑:仅在未攻击时触发
    if Input.is_action_just_pressed("ui_down") or Input.is_action_just_pressed("s"):
        if not attacking:
            attacking = true
            $Area2D/CollisionShape2D.disabled = false
            anim.play("Sword Attack")  # 用play确保动画从头完整播放
    
    move_and_slide()

func stamina_value_player(value):
    stamina = value
 
func _on_animation_player_animation_finished(anim_name):
    frontflipping = false
    # 攻击动画结束后重置状态
    if anim_name == "Sword Attack":
        attacking = false
        $Area2D/CollisionShape2D.disabled = true


func _on_sword_hit_area_entered(area):
    if area.is_in_group("Ghost_Hitbox"):
        area.death()


func _on_area_2d_body_entered(body):
    if body.is_in_group("Hit"):
        body.take_damage()
    else:
        pass

关键修改说明

  • 移除_physics_process开头的attacking = false,避免攻击状态被强制重置
  • 攻击触发逻辑改为仅在not attacking时生效,防止重复触发
  • 所有非攻击动画的播放条件都添加and not attacking,确保攻击过程不被打断
  • 通过animation_finished信号监听攻击动画结束,再重置攻击状态和碰撞体开关
  • 攻击动画改用anim.play()而非直接设置current_animation,确保动画从头完整播放

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 08:19:57