+1 vote

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.

Godot version v3.4.2.stable
in Engine by (59 points)

1 Answer

0 votes
Best answer
global_position = spawn.global_position

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

by (7,925 points)
selected by

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(globalposition) and spawn.globalposition 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?

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 ?

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.