Godot Version
v4.5.1.stable.official [f62fdbde1]
Question
Hello, I have this script that generate animation that shake an object.
@tool
extends AnimationPlayer
@export var target_node2d:Node2D
@export var shake_gen:bool:
set(v):
if v and Engine.is_editor_hint() and target_node2d != null:
generate_shake_animation(_length, _spacing, _magnitude)
@export var _length := 0.5
@export var _spacing := 0.03
@export var _magnitude := 20.0
func generate_shake_animation(length:float, spacing:float = 0.03, magnitude:float = 20.0):
var animation := Animation.new()
animation.length = length
var idx := animation.add_track(Animation.TYPE_VALUE)
var node_path = str(get_parent().get_path_to(target_node2d))
animation.track_set_path(idx,node_path+":position")
animation.track_set_interpolation_type(idx,Animation.INTERPOLATION_CUBIC)
animation.track_insert_key(idx,0.0,target_node2d.position)
var current_position := spacing
var last_track_id := -1
while current_position < length:
var mag := lerpf(magnitude,0.0,current_position/length)
last_track_id = animation.track_insert_key(idx,current_position,
Vector2(
randf_range(-mag,mag),
randf_range(-mag,mag)
)
)
current_position += spacing
if last_track_id != -1:
if is_equal_approx(current_position, length):
animation.remove_track(last_track_id)
animation.track_insert_key(idx,length,target_node2d.position)
# append animation
var animation_library := get_animation_library("")
if animation_library.has_animation("shake"):
animation_library.remove_animation("shake")
animation_library.add_animation("shake",animation)
This script used to work on older version of Godot 4. But now it doesn’t.
If I tried to make position track, it will instead create a new track with exact same name and path.
Why is that? What changes? Is there a way to fix it? I’m not sure where the it goes wrong. I wonder if it’s a bug?
it supposed to look like this:
==============
Another note, I always wanted to create script that can shake object on demand instead of generating animation track like this. But nothing beats those cubic interpolation! However I have no idea how to re-create those interpolated move nvm I found Vector2 has cubic interpolation.
Still!! I will be generating more animation tracks through script in the future other than this shaker, so I still wanted to fix my problem.


