Hello all,
I am working on my first godot game, and would like the enemies to stop moving when they take damage for around 1 second. I cannot figure out how to do this, so if anyone has any ideas, please help me out!

Here is my enemy movement and damage code:

var speed = 50
var motion = Vector2.ZERO
var player = null


func _physics_process(delta):
	motion = Vector2.ZERO
	if player:
		motion = position.direction_to(player.position) * speed
	motion = move_and_slide(motion)

func handle_hit(damage: int):
	enemy_health -= damage
	if enemy_health <= 0:
		Global.score += 1
		queue_free()

func _on_Area2D_body_entered(body):
	if "Player" in body.name:
		player = body
		_animated_sprite.play("walk")
	else:
		pass

func _on_Area2D_body_exited(body):
	if "Player" in body.name:
		player = null
		_animated_sprite.play("idle")
	else:
		pass

Cheers,
Eli

  • MikeCL replied to this.
  • EliSteiny
    There are different ways to do it.

    Here's an easy way (not sure if it's the best)
    Add a timer to your enemy

    onready var stunned_timer = $Timer
    var is_stunned = false
    
    func _physics_process(delta):
    	motion = Vector2.ZERO
            if is_stunned:
                    return
    
            if player:
    	        motion = position.direction_to(player.position) * speed
    
    	motion = move_and_slide(motion)
    
    func handle_hit(damage: int):
            enemy_health -= damage
    	if enemy_health <= 0:
    		Global.score += 1
    		queue_free()
    
            #When the enemy gets hit, you set the stunned variable to true and start the timer
            is_stunned = true
            stunned_timer.start()
    
    #Connect this to the timeout signal on your Timer
    func on_timeout():
             is_stunned = false

    You could also implement a finite state machine pattern. But that's a bit deeper than the solution above.

    EliSteiny
    There are different ways to do it.

    Here's an easy way (not sure if it's the best)
    Add a timer to your enemy

    onready var stunned_timer = $Timer
    var is_stunned = false
    
    func _physics_process(delta):
    	motion = Vector2.ZERO
            if is_stunned:
                    return
    
            if player:
    	        motion = position.direction_to(player.position) * speed
    
    	motion = move_and_slide(motion)
    
    func handle_hit(damage: int):
            enemy_health -= damage
    	if enemy_health <= 0:
    		Global.score += 1
    		queue_free()
    
            #When the enemy gets hit, you set the stunned variable to true and start the timer
            is_stunned = true
            stunned_timer.start()
    
    #Connect this to the timeout signal on your Timer
    func on_timeout():
             is_stunned = false

    You could also implement a finite state machine pattern. But that's a bit deeper than the solution above.

      MikeCL That did exactly what I wanted it to! Thank you so much, I appreciate it alot!

      Just a comment, if its just the speed, normally its called "snare" , not stun. 🙂