There are many ways of making this work depending on how you want every little thing to work but you can try looking into State Machines.
https://www.youtube.com/watch?v=ow_Lum-Agbs
In this video we'll walk though the setup and creation of a Finite State Machine (FSM) in Godot 4.
Finite State Machines are incredibly useful for managing simple states and transitioning between them. For this video we will look at an enemy with the states of wandering, following, and attacking. Then, we'll build the wander and follow states t...