My Enemy wont move when starting a my game but i have no errors and includet its movement in my code

The enemy should only track the player in the radius of area 2d that works like its supposed to the problem is that no own movement is appied to the enemy when it is loaded in so it stand still until persue_player is true and later false again.

Try my tweaked code that I wrote up for you. (I changed var names, so those will either need to be changed back or the var names need to be changed.

will do now

Another thing I noticed, at start are your rays both not detecting wall and detecting floor, either right or left? If not your else statement code won’t set direction. You can fix this by using if/elif/else. Where the if/elif are your raycasts and else is Vector2(1,0).

At the start only the two floor raycast are detecting. So i input ur code but changed the names back bc animator wasnt declared in scope so i couldnt use it. Your Code works exactly the same as mine and shares the same problem where it doesent apply movement when launching the game only after triggering persue_player. It currently looks like this: extends CharacterBody2D

var speed = 40
var direction = Vector2( 1, 0)
var pursue_player = false
var player = null

@onready var animated_sprite_2d: AnimatedSprite2D = %AnimatedSprite2D
@onready var collision_shape_2d: CollisionShape2D = $DetectionArea/CollisionShape2D
@onready var ray_cast_stick_to_ground: RayCast2D = $Location/RayCastStickToGround
@onready var ray_cast_floor_right: RayCast2D = $Location/RayCastFloorRight
@onready var ray_cast_wall_right: RayCast2D = $Location/RayCastWallRight
@onready var ray_cast_floor_left: RayCast2D = $Location/RayCastFloorLeft
@onready var ray_cast_wall_left: RayCast2D = $Location/RayCastWallLeft

func _physics_process(delta):
if ray_cast_stick_to_ground.is_colliding():
position.y = ray_cast_stick_to_ground.get_collision_point().y

if pursue_player:
	direction = (player.position - position).normalized()
else:
	if not ray_cast_floor_right.is_colliding() or ray_cast_wall_right.is_colliding():
		direction = Vector2( -1 , 0) #Flip the movement direction
	if not ray_cast_floor_left.is_colliding() or ray_cast_wall_left.is_colliding():
		direction = Vector2( 1 , 0) #Flip the movement direction
	if direction.x < 0:
		animated_sprite_2d.flip_h = true
	elif direction.x > 0:
		animated_sprite_2d.flip_h = false
	animated_sprite_2d.play("walk")
	velocity = direction * speed
	move_and_slide()

func _on_detection_area_body_entered(body):
player = body
pursue_player = true

func _on_detection_area_body_exited(body):
player = null
pursue_player = false

how do i implement a gravity system isnt there already gravity in the Projekt settings? (Like i said im fairly new to this stuff, about a week and a half to be exact)

That isn’t the code I wrote exactly, those last 7 lines shouldnt be indented, they don’t go under your else statement.

Remove the indentation on these lines

if direction.x < 0:
	animated_sprite_2d.flip_h = true	
elif direction.x > 0:		
    animated_sprite_2d.flip_h = false	
animated_sprite_2d.play("walk")	
velocity = direction * speed	
move_and_slide()

Write print(direction) right before velocity.

Like this?

func _physics_process(delta):
if ray_cast_stick_to_ground.is_colliding():
position.y = ray_cast_stick_to_ground.get_collision_point().y

if pursue_player:
	direction = (player.position - position).normalized()
else:
	if not ray_cast_floor_right.is_colliding() or ray_cast_wall_right.is_colliding():
		direction = Vector2( -1 , 0) #Flip the movement direction
	if not ray_cast_floor_left.is_colliding() or ray_cast_wall_left.is_colliding():
		direction = Vector2( 1 , 0) #Flip the movement direction
if direction.x < 0:
	animated_sprite_2d.flip_h = true
elif direction.x > 0:
	animated_sprite_2d.flip_h = false
animated_sprite_2d.play("walk")
print(direction)
velocity = direction * speed
move_and_slide()

I can’t tell if indentation is correct, use formatting ``` ← so I can see indentation, but the print statement is in the right place.

Thanks for editing, yes.

OMG it prints direction = ( 0, 0) all the time but i dont get why bc in the var direction it sais Vector2( 1, 0)

Okay, now place that print statement under your else statement.

under else it sais ( 1, 0)

i moved the velocity further up in the function tree and now it moves when starting the game how its supposed to BUT now it doesent change direction when hitting a cliff exept when pursue_player was activated before. Heres how it looks : func _physics_process(delta):
if ray_cast_stick_to_ground.is_colliding():
position.y = ray_cast_stick_to_ground.get_collision_point().y

if pursue_player:
	direction = (player.position - position).normalized()
else:
	print(direction)
	velocity = direction * speed
	if not ray_cast_floor_right.is_colliding() or ray_cast_wall_right.is_colliding():
		direction = Vector2( -1 , 0) #Flip the movement direction
	if not ray_cast_floor_left.is_colliding() or ray_cast_wall_left.is_colliding():
		direction = Vector2( 1 , 0) #Flip the movement direction
if direction.x < 0:
	animated_sprite_2d.flip_h = true
elif direction.x > 0:
	animated_sprite_2d.flip_h = false
animated_sprite_2d.play("walk")
move_and_slide()

Try writing print(direction) under both statements under else where you change direction.

Print(direction, “first”) and print(direction, “second”)

Also, move velocity back to where it was before, you need to figure out why/where direction is being set to 0.

Also try setting second if statement under else to elif.

i moved velocity back above move and slide but i keept the if bc elif made it so it didnt print anything. Heres what it printed right at the start :
(-1.0, 0.0)first
(1.0, 0.0)second
(-1.0, 0.0)first
(1.0, 0.0)second

yes it printed it twice but i dont get why

after that it didnt print anything and i wonder why isnt it moving then when there is a direction that isnt 0,0.

i got the enemy setup for you

Are your rays colliding with your area2d at start? If you place print statement inside your func that respond to area2d collision does it activate at start?

Something is setting your direction to 0. At the moment the most likely is that the enemy is activating your area2d collisions with its rays.

Did you put a print(direction) under your if statement to see if persue_player: is triggering at start?

If this is the case this can be prevented with a guard statement in your area2d functions. If you make your player have class_name Player then in your two area2d functions place if not body is Player: return at start of function

so yes area 2d gets triggert at the start 2 times witout me entering it and so does pursue_player as a reaction

Okay, then you need to create a guard statement in your area2d functions.