How to unlink Tilemap from Player Camera?

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

Hello Everyone,

TLDR: How can I make sure, that the coordinates of my tilemap are the same in debug mode and scene view, no matter where my camera is located in debug mode. It looks like in debug mode, the tile in the top left of the game screen is always at position 0,0, no matter where my camera is located.

My friend and I are working on our first Godot game. The player is supposed to move around in a tilemap by clicking on a tile next to the tile it stands on. We pretty much had that figured out.

Now we wanted to add a camera which follows the player, so the player is always in the middle of the screen. I added a Camera2D to our Player Scene and set current to true.

The problem we now have is that the tilemap and the position of the tiles seems to be linked to the camera. That means, no matter where on the map the camera is, the top left tile is always on position 0,0.

The way we set up our camera initially is as follows:

  1. The player is spawned on the tile with the tile ID 0. That’s how we make sure our player always starts on our “starting tile”.
  2. After that, the Camera2D position is changed to the spawn point once (just having current checked in the Camera2D didn’t do the trick, the camera would just stay at the initial location of the player).
  3. Then, our player is supposed to be able to move to adjacent tiles.

This is where the problem becomes visible. We set up the player in a way, so he can only move to tiles, which actually have a tile placed in the respective position. In other words, the player cannot move to tiles which are empty (or have the tile ID -1).

I believe because of the camera movement, the tilemap we see and the actual tile map coordinates become desynched. I printed out the tile position when I click on a tile. The coordinates which show are different from the coordinates of the selected tile in the scene view. In fact it really looks like the coordinates which are printed out, are relative to the current screen. Whenever I click in the upper left corner of my screen, it prints 0,0.

I tried putting the tilemap node into a CanvasLayer node, but it seemed to make things really weird.

I imagine this can be quite confusing when reading, but I hope the actual problem became clear. Please let me know if you need further explanation or information.

Thank you for all your help in advance!


:bust_in_silhouette: Reply From: klaas

you scene tre should look something like this

→ Player
-----> Camera
→ Tilemap

… so the camera is a child of player. Then the cameras position is relative to your player. Moving the player moves the camera with him.

Hey Klaas,

Thank you for your reply. This is what my scene trees look like:

Map Scene Tree
Inside this scene is the Player as you can see. The player’s tree looks as follows:

Player Scene Tree
Here you can see the camera.

I guess that is pretty much how you described it, only in two scenes? Thank you for looking into it!


Malthur123 | 2020-08-30 10:47