-1

i was wondering if there's a function in godot that allows we to see where in the screen or specifically in what cardinal direction based on the screen the cursor is in. i was wondering if such a function exists as i'm currently creating a 2d isometric game similar to ROTMG/realm of the mad god and i would like to know how i could make the sprite of the player change based on what cardinal direction/part of the screen the cursor is located. In short i'm inquiring if there's a function in godot that locates the cursor and tells me where it is located through cardinal directions i.e. northeast part of the screen

  • There is no specific function to do that in Godot, as "Cardinal" directions aren't really defined in game engines. What you could do is check the mouse position ( `get_global_mouse_position()` ) with a series of `if` statements to determine what quadrant of the screen it is in. ( Ex: `var cardinal; var mousePos = get_global_mouse_position(); if mousePos.x < 500 && mousePos.y < 500: cardinal = west; ` or something like that.) – Christopher Bennett Jun 01 '20 at 12:58

1 Answers1

0

If your sprite is a Node2D, you can use get_local_mouse_position() in the script attached to your sprite to check where the mouse position is relative to it.

From there, you can find the angle using angle_to_point(mouse_position) with the sprite's position and mouse position.

Travis
  • 1
  • is it correct that the results i'm getting range from just -3 to 3?? – The new guy Jun 02 '20 at 09:05
  • For `angle_to_point()`, that's expected since the return value is in radians, not degrees. Use `rad2deg()` to convert it to degrees for display, and `deg2rad()` for the opposite. – Calinou Jun 03 '20 at 07:09