What do you want to do? Is it in 2D or in 3D? Should the object smoothly move towards the mouse or teleport to it?
I am going to assume the most simple case: a 2D constraint where the object teleports.
What you have to do is simple:
- Get the constrained object position relative to the area center,
- constraint this position to the radius if it is too big,
- place the constrained object at the calculated position.
Here is a script that does that:
# This is the "constrainer" script
# You can move the constrainer node to move the area center.
## The radius of the constrained area.
export var radius := 30 setget set_radius
export var constrained_object_path: NodePath
onready var constrained_object: Node2D = get_node(constrained_object_path)
func _process(_delta: float) -> void:
# 1. Get the constrained object position relative to the area center
var global_dir := get_global_mouse_position() - global_position
# 2. constraint this position to the radius if it is too big
if global_dir.length() > radius:
global_dir = radius * global_dir.normalized()
# 3. place the constrained object at the calculated position
constrained_object.global_position = global_position + global_dir
func _draw() -> void:
draw_circle(Vector2.ZERO, radius, Color(1.0, 0.0, 0.0, 0.25))
radius = r
update() # this triggers a _draw() call.