How do i make my enemy wait for 0.5s before his next move

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Meno

Hellow, i’m new in the godot community and for a game jam, i would like to make a small puzzle game where you have to avoid enemies. Thoses enemies are making some back and forth in a restricted area.

The problem’s here is that i would like between each movement, the enemy have to wait a small amount of time a little like in Cadence of hyrule.

I tried to code this but it don’t really work

Thanks (sorry if there’s some mistakes i’m french)

The code :
`extends KinematicBody2D

export var direction = 1 #1 = right, -1 = left
export var speed = 32 #The size of one tile, the distance that the enemy have to travel
export var stamina = 4 #The number of tile it can travel

var max_stamina = stamina * speed
var curent_stamina = 0 #To know how much tile it have already travel

func _physics_process(delta):

move()

func move():

var vel = Vector2()

if curent_stamina < max_stamina: 
	curent_stamina = curent_stamina + 32 #It travel 1 tile for now
	vel.x = speed * direction 
	vel = move_and_slide(vel)
elif curent_stamina == max_stamina: #If it travel the max stamina, it changes his direction
	direction = direction * -1 
	curent_stamina = 0 

`

:bust_in_silhouette: Reply From: timothybrentwood

Create a Timer:

var move_timer : Timer

func _ready():
    move_timer = Timer.new()
    self.add_child(move_timer)
    move_timer.wait_time = 0.5
    move_timer.one_shot = true # may want to change this

func move():

    var vel = Vector2()
    if not move_timer.is_stopped(): # can't move yet, just return
        return
    elif curent_stamina < max_stamina:
        ....
    elif curent_stamina == max_stamina:
        ....
    move_timer.start() # start timer if we moved

Thanks that helped me a lot !

Meno | 2021-07-02 18:01