Godot Version
v4.3.stable.steam
Question
Hi, I’m making a boss fight. And I have a problem. Now I’m making its last stage where the hand moves left and right and if it detects the player, then the animation of the hand lowering should play. I make all the animations through AnimationPlayer

The problem is that I don’t know how to pass the position.x value to the key on the timeline.
That is, I want to make it so that as soon as the hand position = the player’s position by x, the variable is assigned $Player.position.x and then immediately transferred to the key of my animation, which will start from the desired place
I saw that AnimationPlayer has something like track_set_key_value
but I don’t understand at all how it works, for example, how can I edit a specific key of a specific animation!
Maybe my method is not quite correct, if you know a better method, please write, I really ask for help.
1 Like
I HIGHLY suggest you use a Tween for animations in which you’re not aware of the final value(Or in this case, the X coordinates we’re falling at). I’ll show you with a little example of my own.
-
- I created a simple knife scene with a simple
Sprite
and a RayCast2D
. and gave it a script with some Tweening.
-
- Then I just made sure the player has an area2D for collision detection.(But you can change the code to suit your own needs.
extends Sprite2D
@onready var ray = $RayCast2D
var swing_tween: Tween
var falling_tween: Tween
var can_detect = true
func _ready():
start_swing()
func start_swing():
swing_tween = create_tween().set_loops()
swing_tween.tween_property(self, "position:x", position.x + 50, 1.0)
swing_tween.tween_property(self, "position:x", position.x - 50, 1.0)
func reset_detection():
can_detect = true
func _physics_process(_delta):
if ray.is_colliding() and ray.get_collider().get_parent().name == "Player" and can_detect:
can_detect = false # Prevent further detection until reset
if swing_tween:
swing_tween.kill()
falling_tween = create_tween()
falling_tween.tween_property(self, "position:y", position.y + 35, 0.3)
falling_tween.tween_property(self, "position:y", 40, 0.2)
falling_tween.tween_callback(start_swing)
# Add delay before allowing detection again
falling_tween.tween_interval(1.0) # 2 second cooldown
falling_tween.tween_callback(reset_detection)

Here’s the Node setup if you’re confused:

Oh yeah! This is what I was looking for. You even repeated this scene for me!! I am so grateful. Sorry if there were any mistakes (I use a translator) Ты лучший, дай бог здоровья!
1 Like