• Godot Help2D
  • How to chase the enemy from a distance, and retreat if the player gets to close

Hello, i am trying to make a bullet hell with enemies that all behave differently. I want the weakest enemy to chase the enemy until in attack range, attack, and if the player gets too close, fall back. I have tried making a solution to the problem myself, but my attempts have not garnered success, so i look to the all-knowing gods of the forums for salvation

oh yeah and here's my code, almost forgot to place it

extends KinematicBody2D

var speed = 300
var velocity = Vector2.ZERO
onready var enemy = get_parent()
var player = null
var attack_range = 350




	
	
	

func _on_Area2D_body_entered(_body):
	player = _body
		


func _on_Area2D_body_exited(_body):
	player = null
	
	
func _physics_process(_delta):
	
	if player.global_position.x and player.global_position.y > attack_range:
		var velocity = (player.position - position).normalized() * speed
		move_and_slide(velocity)

(that var velocity in the physics process is a failed attempt at solving it)

I'd probably do something like this.

- controlling object determines which mob is weakest
  - set attack flag on mob

- if attack flag is set
  - check distance to player
  - if distance < too_close
    - move away (many possibilities)
  - else if in firing range
    - fire
  - else
    - move closer by following direction_to()