Godot Version
4.3
Question
I’m trying to make an animation with code. Here’s a snippet I wrote to see if I can make it work. This snippet will play an animation of the character moving right.
extends CharacterBody2D
const movespeed := 30000
func _physics_process(delta: float) → void:
velocity = Vector2.ZERO velocity.x = Input.get_axis('ui_left', 'ui_right') velocity.y = Input.get_axis('ui_up', 'ui_down') velocity = velocity.normalized()*movespeed*delta move_and_slide() var anim_player = get_node('anim_player') anim_player.play('anim_library/move_right')
func _ready():
var anim_player = AnimationPlayer.new() anim_player.set_name('anim_player') add_child(anim_player) var anim_library = AnimationLibrary.new() anim_library.set_name('anim_library') anim_player.add_animation_library('anim_library', anim_library) var anim = Animation.new() var frame_length = 0.5 anim.length = frame_length*6 var frame_coord_track = anim.add_track(Animation.TYPE_VALUE) anim.track_set_path(frame_coord_track, "Node2D/c_body:frame_coord") anim.track_insert_key(frame_coord_track, 0.0, Vector2(0,6)) anim.track_insert_key(frame_coord_track, frame_length, Vector2(1,6)) anim.track_insert_key(frame_coord_track, frame_length*2, Vector2(2,6)) anim.track_insert_key(frame_coord_track, frame_length*3, Vector2(3,6)) anim.track_insert_key(frame_coord_track, frame_length*4, Vector2(4,6)) anim.track_insert_key(frame_coord_track, frame_length*5, Vector2(5,6)) anim.set_loop_mode(1) anim.set_name('move_right') anim_library.add_animation('move_right', anim)
The path to target Sprite2D is Node2D/c_body (Node2D is a direct child of the parent characterbody2d)
The animation doesn’t play. Can anybody help me with this issue?
*** SOLVED ***
“Node2D/c_body:frame_coord” → “Node2D/c_body:frame_coords”
(frame_coord → frame_coords)