How to fix the global offset position?

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

Hey guys im pretty new to godot, and Im having this weird problem where if the node isnt on 0,0 then its creating an offset, ill have a video linked below showing what im talking about, do you have any ideas on how to fix it? Thanks!

It probably has something to do with local positions (relative to parent) and global positions.

exuin | 2020-10-10 04:33

do you have any ideas on how to fix it? all of the nodes are linked and they’re getting the global position of the mouse

veridical | 2020-10-10 04:37

I guess you could try using the to_local method to change the coordinates.

exuin | 2020-10-10 06:24

no dice yet :confused: ill keep trying though, i might just have to start at 0,0 then move it in code to the location i want

veridical | 2020-10-10 06:44

I’m not really sure how moving it in code would help, since the end result would still be that the node’s coordinates are different than global coordinates.

exuin | 2020-10-10 07:05

Would you mind providing a code snippet describing the general idea of how you add the lines? I think the issue may lie in there.

netimu | 2020-10-10 18:18

:bust_in_silhouette: Reply From: kmsxkuse

You may be using a function that takes relative position instead of global / location position.

Without the actual code you’re using, I’ll try to answer the best I can.

Subtract the position you’ve recieved from input from the position of the object you’re trying to move. That will give you a relative position using the object as the origin instead of the global origin.

EDIT: After a lot more googling, try using get_global_mouse_position() for mouse click position.

:bust_in_silhouette: Reply From: nexero
var mos_pos
func _physics_process():
     mos_pos = to_local(get_global_mouse_position())

And then set this mos_pos to object.
and if it still not working then

obj.global_position = to_global(mou_pos)

I dont know why this is happening. But it worked for me.