How can I make an enemy that follows my player(2D Platformer) ?

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

Hello guys I am working on a 2D game and It’s very simple with normal enemies who just keep moving around. I followed various tutorials and well as videos to reach at this stage but now when I want to go further and create complex enemies, I am not finding any good tutorials. This is my player script :

extends KinematicBody2D

const UP=Vector2(0,-1)
const GRAVITY=20
const ACCELERATION=50
const MAX_SPEED=150
var JUMP_HEIGHT=-400
const BULLET=preload("res://Bullet.tscn")
var motion =Vector2()
const TYPE="player"
const max_bullets=50
var remaining_bullets=max_bullets
var jumpcount=0



onready var scorelabel = get_parent().get_node("Score/ScoreDisplay")
onready var bulletlabel = get_parent().get_node("BulletCount/BulletDisplay ")

var playerhp=10
var hpboost=2
var score=0

var is_dead=false

func _physics_process(delta):
	if is_dead==false:
		motion.y+=GRAVITY
		var friction=false
		
		if Input.is_action_pressed("ui_right"):
			motion.x=min(motion.x+ACCELERATION,MAX_SPEED)
			$Sprite.flip_h=false
			$Sprite.play("Run")
			if sign($Position2D.position.x)==-1:
				$Position2D.position.x*=-1
		elif Input.is_action_pressed("ui_left"):
			motion.x=max(motion.x-ACCELERATION,-MAX_SPEED)
			$Sprite.flip_h=true
			$Sprite.play("Run")
			if sign($Position2D.position.x)==1:
				$Position2D.position.x*=-1
			
		else:
			motion.x=0
			$Sprite.play("Idle")
			friction=true
			motion.x=lerp(motion.x,0,0.2)
		
		if is_on_floor():
			if Input.is_action_just_pressed("ui_up"):
				if JUMP_HEIGHT==-600:
					jumpcount+=1
					print(jumpcount)
				if jumpcount==6:
					JUMP_HEIGHT=-400
				motion.y=JUMP_HEIGHT
			if friction == true:
				motion.x=lerp(motion.x,0,0.2)
				
		else:
			$Sprite.play("Jump")
			if friction == true:
				motion.x=lerp(motion.x,0,0.5)
		
		if Input.is_action_just_pressed("ui_focus_next"):
			remaining_bullets-=1
			var bullettext="Bullets :" +String(remaining_bullets)
			bulletlabel.clear()
			bulletlabel.add_text(bullettext)
			if remaining_bullets>0:
				var bullet=BULLET.instance()
				if sign($Position2D.position.x)==1:
					bullet.set_bullet_direction(1)
				else:
					bullet.set_bullet_direction(-1)
				get_parent().add_child(bullet)
				bullet.global_position=$Position2D.global_position
			else:
				remaining_bullets=1
				
		motion=move_and_slide(motion,UP)
		
		if get_slide_count()>0:
			for i in range(get_slide_count()):
				if "Enemy" in get_slide_collision(i).collider.name:
					dead()
	
	if motion.y>1000:
		get_tree().reload_current_scene()

func dead():
	playerhp-=1
	if playerhp<=0:
		is_dead==true
		$Sprite.play("parlok")
		motion=Vector2(0,0)
		$CollisionShape2D.disabled=true
		$Timer.start()
	
	
	
	



func _on_Timer_timeout() -> void:
	get_tree().change_scene("StartMenu.tscn")





func _on_Area2D_body_entered(body: Node) -> void:
	score+=1
	var scoretext="Score : "+String(score)
	scorelabel.clear()
	scorelabel.add_text(scoretext)
	



func _on_Bulletinc_area_body_entered(body: Node) -> void:
	remaining_bullets+=20
	if remaining_bullets>50:
		remaining_bullets=max_bullets





func _on_HealthBoost_body_entered(body: Node) -> void:
	playerhp+=hpboost
	if playerhp>10:
		playerhp=10
	



func _on_Long_Jump_body_entered(body: Node) -> void:
		JUMP_HEIGHT=-600

	

How can I make an enemy who will follow my player ? All I have right now are enemies like this :

extends KinematicBody2D
const GRAVITY =10
export(int) var SPEED =20
const FLOOR=Vector2(0,1)

var velocity=Vector2()

var direction=1

var is_dead=false
export(int) var hp=10


func _ready() -> void:
	pass # Replace with function body.

func dead():
	hp-=1
	if hp<=0:
		is_dead=true
		velocity=Vector2(0,0)
		$AnimatedSprite.play("Dead")
		$CollisionShape2D.disabled=true
		$Timer.start()


func _physics_process(delta):
	if is_dead==false:
		velocity.x=SPEED*direction
		if direction == 1:
			$AnimatedSprite.flip_h=false 
		else:
			$AnimatedSprite.flip_h=true
		$AnimatedSprite.play("Corona")
		velocity.y+=GRAVITY
		velocity=move_and_slide(velocity, FLOOR)
	
	if is_on_wall():
		direction=direction*-1
		$RayCast2D.position.x*=-1
		
	if $RayCast2D.is_colliding()==false:
		direction=direction*-1
		$RayCast2D.position.x*=-1
		
	if get_slide_count()>0:
			for i in range(get_slide_count()):
				if "Player" in get_slide_collision(i).collider.name:
					get_slide_collision(i).collider.dead()


func _on_Timer_timeout() -> void:
	queue_free()



	
:bust_in_silhouette: Reply From: Orys

Here is a flying enemy script from HeartBeast’s tutorial :
It’s really simple because it doesn’t care about ground/walls etc. but maybe it can help you

# Contains some simple data like MAX_SPEED
extends "res://Enemies/Enemy.gd"

export(int) var ACCELERATION = 100
# Stores the Player and WorldCamera
var MainInstances = ResourceLoader.MainInstances

onready var sprite = $Sprite

func _ready():
    # Disable movement when ready and enables it with a VisibilityNotifier2D (enemy starts chasing you when he's near you)
	set_physics_process(false)

func _physics_process(delta):
	var player = MainInstances.Player
	if player != null:
		chase_player(player, delta)

func chase_player(player, delta):
    # You get the direction by doing PlayerPosition - EnemyPosition.
    # You get a vector that points to  the player and you normalize it (which means it reduces its magnitude to 1)
	var direction = (player.global_position - global_position).normalized()
enter code here
	motion += direction * ACCELERATION * delta
	motion = motion.clamped(MAX_SPEED)
    # Flip the sprite so it faces the player
	sprite.flip_h = global_position < player.global_position
	motion = move_and_slide(motion)

func _on_VisibilityNotifier2D_screen_entered():
	set_physics_process(true)