The character won't follow the mouse

Godot Version

I am using Godot 4.4 with GDS

Question

Hi!
I recently restarted my game and I’m trying to add mouse controls to it.
There is a second character that follows the mouse, but it doesn’t work. It moves with the mouse, but isn’t ON the mouse.
Here’s my code:

func _physics_process(delta: float) -> void:
	if is_split and not is_unsplit and not is_splitting:
		#Add the gravity.
		if not is_on_floor():
			velocity.y += gravity * delta

	#Handle jump.
		if Input.is_action_just_pressed("Up") and is_on_floor():
			velocity.y = JUMP_VELOCITY

	#Get the input direction and handle the movement/deceleration.
	#As good practice, you should replace UI actions with custom gameplay actions.
		var direction := Input.get_axis("left", "right")
		if direction:
			velocity.x = direction * SPEED
		else:
			velocity.x = move_toward(velocity.x, 0, SPEED)

		move_and_slide()
	elif is_splitting and not is_unsplit and not is_split:
		hitbox.disabled = true
		position = get_global_mouse_position()
		print(get_global_mouse_position())
		print(position)
		anim.play("idle_invis")
	elif is_unsplit and not is_split and not is_splitting:
		hitbox.disabled = true
		position = wax.position
		gravity = 0
		anim.play("invis")
		
		
	if Input.is_action_just_pressed("Split and Rejoin") and is_unsplit and not is_split and not is_splitting:
		is_splitting = true
		print("splitting")
		is_unsplit = false

Thanks!

1 Like

You may be mixing global and local coordinates, setting global_position = get_global_mouse_position() may work better.

Thanks! That worked perfectly!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.