Moving the Player with Mouse Controls

Godot Version

4.4.1 Stable

Question

Yo, I’m new, what can I do to move the player by moving the mouse? Here’s what I have so far, I used the code from the Mouse and input document page

Keep in mind, this is a 2D Game

var x
var y

func _input(event):
	# Mouse in viewport coordinates.
	if event is InputEventMouseButton:
		print("Mouse Click/Unclick at: ", event.position)
	elif event is InputEventMouseMotion:
		print("Mouse Motion at: ", event.position)
		x = event.relative.x
		y = event.relative.y

	# Print the size of the viewport.
	print("Viewport Resolution is: ", get_viewport().get_visible_rect().size)

Okay, are you trying to move it by the movement of the mouse or by the clicking of the mouse? If its by clicking, do you know how to use the input map?

1 Like

If you want to move the node you’ll have to change it’s position or global position properties. Something like global_position += event.relative

1 Like

Direct link to Godot Docs that talks about 2d mouse movement :

1 Like

The movement of the mouse basically. Move it to the target then click it to shoot.

if you want your player to move to the mouse position when mouse is clicked. send the mouse position to player when mouse click, let player calculate the direction to move by (mouse_pos - self.pos).normalize() (this is pesudo code) and move in that direction until mouse_pos is reached.

if you want your player to “follow“ your mouse, do the samething but the input event is now InputEventMouseMotion. I dont understand why you are using event.relative here.