• Godot Help
  • How Get Mouse Pointer's position in the world | Godot 2D

Hello everyone,

I'm working on a city-building game where the players will make their unique cities by placing various buildings on a procedurally generated world map. I'm done with the map generation, but I'm stuck with putting buildings on the land.
as you can see in the image, I've selected the tile at position (-21, -21) and when I convert the position from map to world I get the coordinates as follows: (-672, -672). But when I click on that spot with my mouse and try to get the position of the pointer I get x and y coordinates around 60 or 50 which is with respect to the viewport as it starts from the top left corner. I want to get the value of coordinates respective to the world not to the viewport. Please help. You can answer in both c# and GdScript.

Thanks in advance!!!

  • All the C# functions and GDScript functions are the same. Just GDScript is snake_case and C# is PascalCase. You might have to use GD. for global functions. Like.

    GD.GetGlobalMousePosition();

A copy and paste of the source code is better.

You would need to add the viewports view location onto the mouse position.
So if the viewport was viewing +50 from your world origin, mousex+50.

You might be able use the camera's location for this, unless your moving the world around and camera remains stationary. in that event you would need to use the offset of the world.

    solved! The problem seems to be with c#. I tried this with gdScript and it worked!
    get_global_mouse_position();

    newmodels Hi! Thanks for answering. Do you know the c# alternative for this GDscript method : get_global_mouse_position()

    All the C# functions and GDScript functions are the same. Just GDScript is snake_case and C# is PascalCase. You might have to use GD. for global functions. Like.

    GD.GetGlobalMousePosition();

      cybereality Hi, you are always such a great help! It worked without referencing the GD class. Just GetGlobalMousePosition();