Godot Version
4.3
Question
I am making my first project (excluding just following tutorials) in Godot. I have a Area2D character that can attack in all 4 cardinal directions. When i attack, an animation is played. The problem is that i can still change the direction of the attack mid animation. I want the attack to be locked to a single direction for the duration of the animation. Is it posible to “lock” position/rotation relative to the parent node with code? In this example, the player is the parent of the weapon. Also: excuse the hard-coding of positions, I will fix this later.
Weapon script:
extends Area2D
var is_attacking = false
var attack_ready = true
signal hit(area: Area2D)
func _on_cooldown_timer_timeout() -> void: #When weapon cooldown is over
attack_ready = true
func _on_enemy_1_area_entered(area: Area2D) -> void:
hit.emit(area)
func _ready() -> void: #First frame
position = $StartPosition.position
rotation = $StartPosition.rotation
func _process(_delta: float) -> void: #Every frame
var attack_frame_index: int = $AnimatedSprite2D.frame
if Input.is_action_just_pressed("swing") and attack_ready:
show()
$AnimatedSprite2D.play()
attack_ready = false
$CooldownTimer.start()
if Input.is_action_pressed("player_right"):
rotation = $StartPosition.rotation
position = $StartPosition.position
if Input.is_action_pressed("player_left"):
rotation = $StartPosition.rotation
position = $StartPosition.position
if Input.is_action_pressed("player_up"):
rotation = - PI / 3
position.x = -30
position.y = -45
if Input.is_action_pressed("player_down"):
rotation = 2 * PI / 3
position.x = 5
position.y = 40
if is_attacking:
$CollisionShape2D.disabled = false
else:
$CollisionShape2D.disabled = true
if attack_frame_index >= 1 && attack_frame_index <= 3:
is_attacking = true
else:
is_attacking = false