Incorrect behavior with AnimationPlayer

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

Hi everyone,

i’m trying to animate the “polygon” property of Polygon2D node.
At first I tried through tweening, but it seems the Tween method cannot work with “polygon” property and packedArrays in general (apparently this problem was already known → #5501).
Then i tried with AnimationPlayer, and witnessed 2 kind of behaviors :

1 - If you add the AnimationPlayer node and you set the animation of the “polygon” property through tracks and keys with the GUI → it works.

2 - but if you try to do all the previous things programmatically (GDScript) without adding manually the nodes and tracks/keys → it half works : the polygon vertices go abruptly from first to final values, without any interpolation/smoothness (even with the track set to Animation.INTERPOLATION_LINEAR and Animation.UPDATE_CONTINUOUS).

To make sure my code was not wrong, i also tried to animate programmatically “color” and “position” properties of Polygon2D → it worked perfectly.
Below the code i’m using if someone have an idea to how solve this problem :

extends Node2D

var poly
var polyW = 100
var polyH = 100
var polyPoints1:= [Vector2(0,0), Vector2(polyW,0), Vector2(polyW,polyH), Vector2(0,polyH)]
var polyPoints2:= [Vector2(0,0), Vector2(polyW,0), Vector2(polyW*3,polyH*3), Vector2(0,polyH)]
var animPlayer
var anim
var colorTrackName = 'Polygon2D:color'
var moveTrackName = 'Polygon2D:position'
var polyTrackName = 'Polygon2D:polygon'
var track_index0 = 0
var trackIndex1 = 1
var trackIndex2 = 2
var keyTime01 = 0.0
var keyTime02 = 0.5
var polyPosition = Vector2(0, 0)
var polyPositionNew = Vector2(500, 200)
const RED = Color.red
const YELLOW = Color.yellow

func _ready() -> void:
#	*********************************** ADD Polygon2D NODE ***********************************
	poly = Polygon2D.new()
	poly.name = "Polygon2D"
	poly.polygon = polyPoints1
	poly.offset = Vector2(10,10)
	poly.color = RED
	add_child(poly)
#	******************************************* END ******************************************
	
#	******************************** ADD AnimationPlayer NODE ********************************
	animPlayer = AnimationPlayer.new()
	add_child(animPlayer)
#	******************************************* END ******************************************
	
#	************************* ADD 'move' ANIMATION in AnimationPlayer ************************
	anim = Animation.new()
	animPlayer.add_animation("move", anim)
	var animList = animPlayer.get_animation_list()
#	******************************************* END ******************************************

#	********************* ADD 'Polygon2D:color' TRACK and KEYS in 'move' ANIMATION ********************
	anim.add_track(Animation.TYPE_VALUE)
	anim.track_set_path(track_index0, colorTrackName)
	anim.track_insert_key(track_index0, keyTime01, 0)
	anim.track_insert_key(track_index0, keyTime02, 1)
	anim.track_set_key_value(track_index0, 0, RED)
	anim.track_set_key_value(track_index0, 1, YELLOW)
#	******************************************* END ******************************************
#
#	******************* ADD 'Polygon2D:position' TRACK and KEYS in 'move' ANIMATION *******************
	anim.add_track(Animation.TYPE_VALUE)
	anim.track_set_path(trackIndex1, moveTrackName)
	anim.track_insert_key(trackIndex1, keyTime01, 0)
	anim.track_insert_key(trackIndex1, keyTime02, 1)
	anim.track_set_key_value(trackIndex1, 0, polyPosition)
	anim.track_set_key_value(trackIndex1, 1, polyPositionNew)
#	******************************************* END ******************************************
	
#	******************** ADD 'Polygon2D:polygon' TRACK and KEYS in 'move' ANIMATION *******************
	anim.add_track(Animation.TYPE_VALUE)
	anim.track_set_path(trackIndex2, polyTrackName)
	anim.track_insert_key(trackIndex2, keyTime01, 0)
	anim.track_insert_key(trackIndex2, keyTime02, 1)
	anim.track_set_key_value(trackIndex2, 0, polyPoints1)
	anim.track_set_key_value(trackIndex2, 1, polyPoints2)
	anim.track_set_interpolation_type(trackIndex2, Animation.INTERPOLATION_LINEAR)
	anim.value_track_set_update_mode(trackIndex2, Animation.UPDATE_CONTINUOUS)
#	******************************************* END ******************************************

func _process(_delta: float) -> void:
	if Input.is_action_just_released("ui_substract") or Input.is_action_just_released("ui_add"):
		animPlayer.play("move")

P.S : Even though “color” and “position” properties worked perfectly without it, i nonetheless forced “polygon” track with Animation.INTERPOLATION_LINEAR and Animation.UPDATE_CONTINUOUS but the result is the same).

Hopfully someone could help me :slight_smile: