Why the enemys isn't moving

Godot Version

4.3

Question

`I tried to use the code of kidscancode and for some reason the enemy isn’t moving

here’s the code to check:`

extends CharacterBody2D
class_name Enemy

enum States {patrol, chasing, alerting, search, back, dead, attack}

@export var speed = 60.0
@export var path = Path2D

@onready var player_detection: RayCast2D = $"player detection"
@onready var wall_detection: RayCast2D = $"WALL DETECTION"
@onready var label: Label = $Label

var current_state = States.patrol
var offset = 0.0
var direction: int = 1
var target = null
var player = null
var current_patrol_point = 0
var patrol_points = []

func _ready():
	if path:
		patrol_points = path.curve.get_baked_points()

func _physics_process(delta):
	label.text = str(States.keys()[current_state])
	velocity = Vector2.ZERO
	choose_action()
	if target:
		#if target.x > position.x:
			#$Sprite2D.scale.x = 1
		#elif target.x < position.x:
			#$Sprite2D.scale.x = -1
		if current_state != States.attack:
			velocity = position.direction_to(target) * speed

	#if velocity.length() > 0:
		#anim_state.travel("run")
	move_and_slide()

func choose_action():
	#var current_anim = anim_state.get_current_node()
	#if current_anim in attacks:
		#return
	match current_state:
		States.dead:
			set_physics_process(false)
		States.patrol:
			if path:
				#anim_state.travel("idle")
				target = null
				return
			target = patrol_points[current_patrol_point]
			if position.distance_to(target) < 5:
				current_patrol_point = wrapi(current_patrol_point + 1, 0, patrol_points.size())
		States.chasing:
			target = player.position
		States.attack:
			target = player.position
			#anim_state.travel(attacks.pick_random())

#func _on_attack_radius_body_entered(body):
	#current_state = States.attack
#
#func _on_detect_radius_body_exited(body):
	#player = null
	#current_state = States.patrol
#
#func _on_attack_radius_body_exited(body):
	#current_state = States.chasing


func _on_area_2d_body_entered(body: Node2D) -> void:
	player = body
	current_state = States.chasing

It looks like your current state is set to ‘patrol’ initially. Your velocity is set to 0 and it is not changed in your _physics_process (because your target may or may not be set but your current state is not attack.)

I cannot see where your path is set either. It looks like it needs to be set in your editor as it is using an export variable. Have you set a path? If not you will not have any patrol points (set in the ready function).

So your physics_process, as it stands, will only move the enemy if the enemy is in a state of attack, and only if a target is set.

You could try changing your default state to attack, does it move if you do that?

var current_state = States.attack

If it does, the problem is that your logic for behaviour is all messed up. This code is very messy btw, I thought kidscancode was pretty good last time I looked at it. Have you been modifying it.

A top tip is to start again with a fresh scene and start the lesson again, until you are confident that you understand what all the states, targets and paths do and how they interact with each other to produce different movement. I did the introductory 2D game on Godot about three or four times until I knew it almost off by heart. It really helps to repeat things.

Anyway, hope that helps in some way.