Enemy movement controll!

Hi.

Here’s a simple way to do it (assuming your enemy only moves from left to right)

extends Area2D

var direction = 1
var start_position = 0
var walking_distance = 50
var speed = 20

func _ready():
	start_position = position.x

func _physics_process(delta):
	position.x += direction * speed * delta
	
	if (position.x >= start_position + walking_distance):
		direction = -1

	if (position.x <= start_position - walking_distance):
		direction = 1

On the other hand, Brackey’s tutorial explains this with a bit more detail and also adds simple raycasting to check if the enemy has hit a wall (in that case it will also change direction). In minute 42 he explains how to make an enemy roughly like the one you described.

2 Likes