How to teleport to a checkpoint

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

I am trying to make my character teleport to a checkpoint that I placed on the map, the checkpoint is an area 2d. I use “get_position_in_parent()” and set it in a global variable. when a key is entered, the player will teleport the global variable I made, but it is not working. How should I go about this?

Maybe using the global_position of the parent and assigning it to the player’s global_position might work?

godot_dev_ | 2022-08-31 13:59

:bust_in_silhouette: Reply From: Lopy

A Node2D’s position is relative to the position of it’s parent (absolute if the parent is not inheriting Node2D). This means that if your checkpoint and your player have different parents, and those are not in the same place, the same position value inside the checkpoint and the player will correspond to different absolute position.

To avoid issues, use global_position: player.global_position = checkpoint.global_position.


A common issue is that your important nodes (like Area2D) are somewhere you would not expect them to be, with only their children in the right place. When selecting a Node2D in the tree panel of the Editor, a red cross is shown where the node is.

If it is not where you thought, make sure that your CollisionShapes, Sprites, etc have a position of 0 relative to their parent (or small values), and that you move the checkpoint/player itself when you move them around. You can use the “Makes sure the objects children are not selectable” toggle, between the lock and the bone in the bar on top of the 2D view, to make moving the right nodes easier.

Thanks so much :smiley:

GodotUser21 | 2022-09-01 04:38