It works for sprite nodes through AnimationPlayer correctly, but why not for KinematicBody2D nodes ?

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

When I test the animation in the Editor, both the position and rotation are applied, however, when I run the program, only one is applied. Why does the animation run correctly in the Editor and not the resultant program ? I did think as you said that it applies the transform and therefore overwrites any other property changes but why allow individual and multiple positional, rotational and scaling changes through animation if only one is going to be applied ?

:bust_in_silhouette: Reply From: umma

Probably physics is acting on the kinematic body 2d. Try changing it to a static body 2d when animation starts and back to kinamatic body 2d when animation stops.

It does work when it’s a StaticBody2D and not a KinematicBody2D but a moving platform is supposed to be “kinematic” because it’s moving. Anyway, thanks, it seems to work and all collision is also working as expected. It’s just one of things I guess…

zenbobilly | 2022-07-17 07:05

Actually it’s something to do with “sync_with_physics”, however turning it on causes the problem, but turning it off means that the character doesn’t interact with the platform properly and when the platform is a StaticBody2D, the character doesn’t move with the platform as expected. I’ve tried using advance() on the animation in “manual” mode but the problem still occurs. I don’t know if there’s any real solution to this other than to avoid AnimationPlayer.

zenbobilly | 2022-07-17 08:25

So sorry about that, for some reason I automatically think you were animating a bullet. I didn’t realize it was a platformer.

umma | 2022-07-23 22:32

:bust_in_silhouette: Reply From: the_maven

In the Asset Library Projects there is the 2D Physics Platformer (RigidBody), have a look to see how it’s done there.

Platforms like you say I think are supposed to always be RigidBody2D objects.
Instead of use StaticBody try RigidBody2D.

With RigidBody2D you set it to be Rigid, Static, Character or Kinematic. But the code to it is different for the StaticBody2d or KinematicBody2D:

Actually I got it all working with a sort of convoluted workaround. I’ve used a KinematicBody2D for the platform, let the AnimationPlayer execute the transform changes but not to the KinematicBody2D but instead to a Node2D in the same tree. The animation is run in “manual” mode which then allows me to use the resultant transform (from the affected Node2D) to apply to the KinematicBody2D in one go so that both the rotation and position are applied as expected. The transform has to be applied to the KinematicBody2D so that both the sprite and the collider are updated as required.

This approach also means that “sync with physics” can be left on for the KinematicBody2D so that collision works properly as well.

I tried RigidBody2D with all the variations (Rigid, Static, Character, Kinematic), and the only mode that worked was Kinematic but it didn’t move the character correctly when on the platform, that is, the character would slide a little when the platform changed direction. With my convoluted workaround it works absolutely perfectly, so I’m sticking with it unless I find a better solution.

zenbobilly | 2022-07-17 13:52