Need to make hand following to cursor

Hello, i making a game, where character have a stretching arm that following cursor and i a faced problem of hand part. I’m trying to limit it’s range and right now the only working way i know works bad: it’s look like i’m moving mouse too fast for game and hand just got left hanging at it’s place, but when i move cursor to center of it it’s starting to follow cursor if i move it very slow, of course.

extends Area2D

var max_distance = 8

func _physics_process(delta):
	
	var hand_position = get_global_transform().origin
	var cursor_position = get_global_mouse_position()
	
	var distance = hand_position.distance_to(cursor_position)
	
	if distance <= max_distance:
		global_position = cursor_position

func _unhandled_input(event):
	if event.is_action_pressed("left_mouse_button"):
		$hand_sprite.scale.y = 0.3
	else:
		$hand_sprite.scale.y = 0.5

So, is there is some better way i can make this or i just messed up what i have?

im new and not that good so idk if this is works or not but couldnt you just add a colision2d set to a cirlce that has a radius set to the max range and if the mouse isnt in it then it doesnt track? so like

func areaenteered():
a var = true

on areaexit():
a var = false

if var == true:
handposition = global.mouseposition
and if that works then wouldnt you just need to add a var to know when it is in or out

again im new so please correct me if im wrong

1 Like

Not well, but it’s working. When hand goes out of area it’s stops and never moving again. Trying to solve it.

1 Like

Solved. Replaced _area_entered with _mouse_entered.

1 Like

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