Refer to animation keyframe

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Grizly

Maybe someone can help me.
I am developing 2D platformer and I want to add the “Climb” function to my hero. I have implemented this, but this is not quite what I need. I have nine animations in AnimationPlayer. In the seventh animation, the position of the hero changes. How to give a command from the code, if the seventh frame is playing, then change the position. Now the position is changing with a delay of 0.61 sec. This decision is temporary. Sometimes it doesn’t work synchronously. It happens, but it happens very rarely. Here is an example of my code:

func Climb():

if sprite.flip_h == false:
	climb_up = Input.is_action_just_pressed("Right") 
	target = new_position_r.get_global_position()
if sprite.flip_h == true:
	climb_up = Input.is_action_just_pressed("Left")
	target = new_position_l.get_global_position()

if player_state == state.Hang and velocity.y > 0:
	if climb_up:
		is_climb = true
		yield(get_tree().create_timer(0.61), "timeout")
		position = target
if player_state == state.Climb:
	yield(get_tree().create_timer(0.96), "timeout")
	is_climb = false
:bust_in_silhouette: Reply From: DaddyMonster

Well, in the AnimationPlayer node you can add a method call at a particular frame. It works like a signal which calls a method. In that method you can have whether branching you like.

Also, have you considered using AnimationTree? That’s a node that’s there for precisely this sort of animation management.

There are several options, one is to use a BlendTree, so you can tie the animations together, you can blend them, you can add a TimeScale to forwards and backwards, a seek to jump to a particular point, etc. then you can add a script to this node and just call it.

Otherwise there’s 2d and 3d blend spaces which will lerp between states. Or there’s a state machine.

Much easier to use these nodes than code everything. It’s very powerful.

Thanks for your answer! You gave me some ideas for solving my problem.

Grizly | 2021-10-07 08:30