How to achieve an shaking/vibrating Animation on KinematicBodies with Sprites?

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


i’d like to have Zelda-Like moveable Blocks which can be pushed.
While they are being pushed, they should be animated lke they’re vibrating/shaking.
Can this animation be done by adding a Tween (how?), or should i go for ‘updating to random positions while a Timer plays’.
What is the better approach for this structure…?

  • KinematicBody2D
    | - Sprite
    | - CollisionShape2D


:bust_in_silhouette: Reply From: jgodfrey

Assuming the shaking is just for visual effect and doesn’t actually impact game play or the interaction with the blocks, why not simply animate the sprite object itself? As an outline:

  • Create a suitable “shake” animation for the block’s sprite
  • Set it to loop
  • When the block push starts, play the animation using $"yourAnimation")
  • When the block push stops, stop the animation with$AnimationPlayer.stop()

Thank you, to pick the Sprite Child is the key! (Because I’m already using AnimationPlayer to change position of the KinematicBody2D, changing the sprite’s position doesn’t affect the rest like the collider etc…)

Captain.Susuwatari | 2020-02-01 19:05