Having Difficulties with Mouse Position

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

Alright, so essentially, I have an “orb” as a child of my player. I have two Position2D children of my player, labelled “r_pos” and “l_pos”. When the cursor is to the right of the character, I want the orb to be in the position of r_pos, and when the cursor is to the left of my character, I want the orb to be in the position of l_pos. Here is my code:

var mpos

func _process(delta):
	mpos = get_viewport().get_mouse_position()
	if mpos.x >= 0:
		position = get_parent().get_node("r_pos").position
	if mpos.x < 0:
		position = get_parent().get_node("l_pos").position

For some reason, no matter where the mouse is, the orb is always in the position of r_pos. Could someone help me?

:bust_in_silhouette: Reply From: Dlean Jeans

Try:

mpos = get_parent().get_local_mouse_position()

This will give you the mouse position relative to the character instead of the viewport which is what you’re getting.