get_mouse_pos not returning mouse position

team_disposableteam_disposable Posts: 3Member
edited December 1969 in 2D
Hi All,<br /><br />I'm having a crack at making my first game in Godot. It seems like a lovely engine, but I've run in to a problem. <br /><br />I'm trying to build a top down 2d shooter, where the player fires a bullet from their position in the direction of the mouse cursor.<br /><br />I'm using get_mouse_pos to get the mouse position, but it doesn't seem to be giving me the absolute mouse position. Instead, it seems to giving me a varying location in relation to the mouse cursor, which changes in relation to where my character is in the level (this is presumably as the camera tracks the player, so it is looking at a different part of the viewport).<br /><br />I tested this by creating a node which is just an image, and attaching a script to it that runs get_mouse_pos in process.<br /><br />If I move the character over to the left (and the camera scrolls left with him), the reported mouse position drifts in relation to the mouse cursor - i can tell as the image moves.<br /><br />I presume this is something to do with the viewport, but I don't know how to fix it.<br /><br />Is there anything I can do get the absolute position of the mouse cursor? What is the value that get_mouse_pos is actually reporting?<br /><br />Thanks!

Comments

  • danjodanjo Posts: 81Member
    I once stumbled upon this, too. IIRC I ended up using CanvasItem.get_local_mouse_pos() or CanvasItem.get_global_mouse_pos() <br />
  • team_disposableteam_disposable Posts: 3Member
    That worked, thanks!<br /><br />For anyone else trying it, I didn't have to call it from CanvasItem - I just called it from the sprite class I was in (get_global_mouse_pos())<br />
  • danjodanjo Posts: 81Member
    CanvasItem is the only class that implements those functions, you can call it from any class that extends it (e.g. Sprite)<br />See: http://docs.godotengine.org/en/latest/classes/class_sprite.html
Sign In or Register to comment.