Prevent node from going past radius

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

I’m making a joystick and I don’t want the center circle to go past the boundaries of the outer circle.

func _input(event):
	if event is InputEventScreenDrag or (event is InputEventScreenTouch and event.is_pressed()):
		global_position = event.position - radius
		var dist = (global_position).distance_to(get_parent().global_position - radius)
		if dist > bounds:
			position = (position - event.position).normalized() * bounds

The center just merely disappears when dist > bounds because the line after it is returning wrong calculations.
What is the correct way to achieve this? Thanks in advance.

Do you have two nodes in this setup? One for the background boundary and the moving piece (maybe as a child control)?

creativeape | 2022-08-27 23:54

Yeah, and they’re both TouchScreenButtons (so they both don’t appear on PC). The script is in the child node.

MTM828 | 2022-08-28 17:38