How to set_global_pos of KinematicBody2D?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Vladislav Vorobiev
:warning: Old Version Published before Godot 3 was released.

I have 2D sidescroller with room-like level transitioning. When I move my character to some kind of door, I can press a button to teleport to next level. And this is the moment where stuff getting weird.

After I change level, I’m not destroying my player instance, but trying to change player position to another room door position in new level using set_global_pos. For some positions it’s working without any hassle, but certain positions not work: player pos is about ~100 pixels higher than expected…

I have tried to debug this stuff: at the start of _fixed_process() func I get position that I expect([0, 87] in my case), but after calling move(velocity * delta) stuff gets super weird: this func applies some small velocity (like [0, 8.33]), and position becomes [0, 12]! Next fixed process iterations player moves higher and higher until like its ~100 pixels higher as I mentioned above. I just can’t understand this :confused:

Update code for my character is same as in the bottom of Godot KinematicBody article: http://docs.godotengine.org/en/latest/tutorials/2d/kinematic_character_2d.html#moving-the-kinematic-character

Please help me, I already have ragequited like 10 times or so, I just can’t fight this crap anymore.

This screen may help you: http://i.imgur.com/03AGbuf.png

That “door” on the screen has Area2D with CollisionShape2D of Rectangle. It is in different layer than player, so no collision should happen.

:bust_in_silhouette: Reply From: Vladislav Vorobiev

I have fixed this issue by waiting for 10 _fixed_process calls right after changing position.

Thank you, thank you, thank you! After debugging a very similar problem for hours (I wanted to “beam/teleport” my player back to the start of the level after a collision, but using get_pos with KinematicBody2D led to strange collision events with objects hundreds of pixels away), I finally came across this page, and your solution is working fine!

Thomas Karcher | 2016-10-27 23:03