How I can code a Smooth Moving Button when I hover it

Godot Version

godot 4

Question

I want the button to move into a direction smoothly when I hover it. There is no Youube video about it. Please help

Hello, @shiroku! You can use func _process(delta): with is_hovered() method of BaseButton class. For example:

extends BaseButton

func _process(delta):
     if not is_hovered():
          # Cursor is not hover on button
          return
     
     # Cursor is hover on button
     # Do something for change button pos...

So, if you want to move button at concrete position (from start pos to end pos), also use tween. For this you need to modify example code:

extends BaseButton

var cursorOnButton: bool = false

signal cursorEntered
signal cursorLeft

func _process(delta):
     if is_hovered() and not cursorOnButton:
          # Cursor is hover on button
          cursorEntered.emit()
          cursorOnButton = true
          return

     if not is_hovered() and cursorOnButton:
          #Cursor is not hover on button
          cursorLeft.emit()
          cursorOnButton = false

# Connect this function with cursorEntered signal
func _cursor_entered():
     # Do hover tween

# Connect this function with cursorLeft signal
func _cursor_left():
     # Do not hover tween 

For more about tween read official docs.

1 Like

Thanks you

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