How do I move a KinematicBody2D to a Position2D node?

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

I have a KinematicBody2D node and a Position2D node in my main Game scene. I would like to, from gdscript, teleport the KinematicBody2D node to the position I set through the Position2D node.

I have tried:

position = spawn.position

and

global_position = spawn.position

I’m very new to godot so I don’t fully understand the whole node system yet, if there’s a better way to teleport the Kinematic to a pre-defined position placed in the editor please let me know.

:bust_in_silhouette: Reply From: Inces
global_position = spawn.global_position

I would say this is most universal way You can handle relocating.

This doesn’t seem to work. The player’s position is 64,64 and the Position2D is at 1088,320. Yet when running that code the player stays where they are and when printing(global_position) and spawn.global_position I get 64,64 for both of them. Why is the editor saying the position is at 1088,320 but when I print it says 64,64?

SneakySteve | 2022-01-26 12:42

I assumed that “spawn” is your correct reference to position2d.
Are You sure You referenced position2d ?
You mean Position2d is visually in 1088,320 in editor ? Didn’t you use some icon to visualize your position2d ? And perhaps moved only the icon to 1088,320, while the real position2d stayed in place near top left corner of screen ?

Inces | 2022-01-26 18:52

Thanks so much, figured it out by looking closer in the editory. turns out I actually had a duplicate position2d that was at 64,64 which was breaking the code

SneakySteve | 2022-01-26 18:58