Following Mouse

Godot Version

godot 4.7.2.stable

Question

I am trying to make my Eyes sprite follow the cursors position but not be more than the max or min value on the y or x axis. How can i implement this?

extends Sprite2D

@export var min = -2
@export var max = 2

func _process(delta: float) -> void:
	global_position = get_global_mouse_position()

You could use clampf

global_position = get_global_mouse_position().clampf(min, max)

This would end up with a square-style clamp, and not a very far one of at most 2 pixels either direction (unless you change the export variables elsewhere). And given it’s using global position the restricted movement would be to the global origin of 0, 0. It seems like an odd script, if this isn’t what you want could you explain big-picture what you are trying to do?

Im trying to make the eyes look at the mouse cursor while not leaving the players body

Cool! You might want a rounder clamp, this can be found by first finding the direction from this object to the mouse, then multiply the direction by the distance you want

var direction := global_position.direction_to(get_global_mouse_position())
position = direction * max

By assigning position here it should also work anywhere.

Thanks!