Reduce jitter on mouse hover object(read description)

Godot Version



I’m working on a 2d game where I have a series of files in a box, and I’d like the files to sort of slide up when the mouse hovers over them to indicate which file you are currently over, and I have a system that works well for this. Basically, each file is a sprite with an area 2d and a collision shape that takes up about a third of the top of the file sprite. when the mouse hovers over the file, it pops up like I want, but when the mouse exits through the bottom of the collider there is some jitter, because as the mouse goes out the bottom, the file slides back down, sometimes catching up with the mouse and retriggering the slide up. if you place your mouse cursor right on the bottom edge of the collider it’ll jitter like this pretty much indefinitely. attached is my code, any thoughts on fixing this? I thought about implementing a timer so that whenever you exit the collider there’s a cooldown before the mouse is able to trigger it to slide back up, but I’m not sure exactly how to do that.

If you attach this script to a sprite and give it an area 2d and collision box(with the signals set up correctly) it should be pretty easy to recreate this.

extends Sprite2D

@export var openFile: Node2D #not relevant to the issue

var openFileGD = Script #not relevant to the issue

@export var fileSlideAmount = 100 #the distance the file is allowed to slide when you hover over it

var mouseHovering = false #whether or not the mouse is hovering

var startPos = Vector2() #this is where the initial position of the file is saved so it can be slid back to it when the mouse is not on it

var slidePos = Vector2() #this is where the intended destination of the slide is saved

func _ready():
	startPos = self.position #the resting position of the file when the mouse is not interacting with it

	slidePos = Vector2(self.position.x, self.position.y - fileSlideAmount) #the starting position + the amount you'd like it to move vertically when it slides


	openFileGD = openFile.get_script() #not relevant to the issue

func _process(delta):
#essentially if the mouse is in the collider, the sprite moves towards the desired position, but if the mouse exits it, it slides back to the start position
	if mouseHovering == true:
		self.position = self.position.move_toward(slidePos, 6)
		self.global_position = self.global_position.move_toward(startPos, 6)

func _on_area_2d_mouse_entered():
#saves that the mouse is over this file
	mouseHovering = true

func _on_area_2d_mouse_exited():
#saves that the mouse is no longer over this file
	mouseHovering = false

#irrelevant to the issue
func _on_click_area_input_event(viewport, event, shape_idx):
	if (event is InputEventMouseButton && event.pressed):
		openFile.visible = true

never mind I figured it out. I parented the file to the collision box that triggers the slide instead of the other way around and edited the script so only the file is moving but not the hover detection box