Wrong animation and output when running the script

Godot version 4.2.1

New to godot and i’m getting an output I didn’t ask for.
Im making a game, currently I have a short map, a player and an enemy. When running the script the enemy should play its idle animation but instead it plays its jump animation. I’m not getting any error messages either

extends CharacterBody2D

var speed = 50
#gravity
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var player 
var chase = false

func _on_ready():
	get_node("AnimatedSprite2D").play("Idle")

func _physics_process(delta):
	# Add the gravity.
		velocity.y += gravity * delta
		move_and_slide()

#when exiting the right or left side of player detection collision shape stop movement 
func _on_playerdetection_right_body_exited(body):
	chase = false
	player = get_node("../../Player/Player")
	get_node("AnimatedSprite2D").play("Idle")
	if body.name == "Player":
		velocity.x = 0

func _on_playerdetection_left_body_exited(body):
	chase = false
	player = get_node("../../Player/Player")
	get_node("AnimatedSprite2D").play("Idle")
	if body.name == "Player":
		velocity.x = 0

#player detection - when entering left side of enemys collision flip horisontally and chase the 
#player.
func _on_playerdetection_left_body_entered(body):
	print("left")
	get_node("AnimatedSprite2D").play("Jump")
	player = get_node("../../Player/Player")
	if body.name == "Player":
		var direction = (player.position - global_position).normalized()
		if direction.x > 0:
			chase = true
			get_node("AnimatedSprite2D").flip_h = true
			velocity.x = direction.x * 50
		else: 
			velocity.x = 0

func _on_playerdetection_right_body_entered(body):
	print("right")
	get_node("AnimatedSprite2D").play("Jump")
	player = get_node("../../Player/Player")
	if body.name == "Player":
		var direction = (player.position - global_position).normalized()
		if direction.x > 0:
			chase = true
			get_node("AnimatedSprite2D").flip_h = true
			velocity.x = direction.x * 50
		else: 
			velocity.x = 0

the print() functions are for me to see if player collision is working or not. When running the script I get
“right”
“left”
outputted, don’t know why

I want it to play the idle animation when running the script

Any help is greatly appreciated

What if you’ll try something like that:

extends CharacterBody2D

var speed = 50
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var chase = false

@onready var animation = #drag your animation node here
@onready var player = #drag your player node here
@onready var sprite = #drag your enemy's sprite node here

#Then create one Area2d, which will cover area you want your enemy see player

func _on_Area2d_entered(body):
    chase = true

func _on_Area2d_exited(body):
   chase = false

#Then check if it's chasing player

func _physics_process(delta):
    velocity.y += gravity * delta
    move_and_slide()
    if chase == true:
       position += (player.position - position)/speed
       animation.play("Jump")
       if(player.position - position) < 0:
             sprite.flip_h = true
       else:
             sprite.flip_h = false
    else:
       animation.play("Idle")

It will optimize your code a little and might be solution for your problem

extends CharacterBody2D

var speed = 50
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var chase = false

#drag your animation node here
@onready var animation = get_node("AnimatedSprite2D")

#drag your player node here
@onready var player = get_node("../../Player/Player")

#drag your enemy's sprite node here
@onready var sprite = get_node(".")

#Then create one Area2d, which will cover area you want your enemy see player

func _on_playerdetection_body_entered(_body):
	chase = true

func _on_playerdetection_body_exited(_body):
	chase = false

#Then check if it's chasing player

func _physics_process(delta):
	velocity.y += gravity * delta
	move_and_slide()
	if chase == true:
		position += (player.position - position)/speed
		animation.play("Jump")
		if(player.position - position) > 0:
			$AnimatedSprite2D.flip_h = true
		else:
			$AnimatedSprite2D.flip_h = false
	else:
			animation.play("Idle")

Made it like this. When running the script I get an error message for

		if(player.position - position) < 0:

I get the error message" invalids operand ‘vector2’ and ‘int’ in operator ‘<’."
might be because I miswrote something

Oh, I’m not very good at this movement system, try change it for this:

func _physics_process(delta):
	velocity.y += gravity * delta
	move_and_slide()
	var direction = (player.position - position).normalized()
	if chase == true:
		velocity.x = direction.x * speed
		if direction.length() < 0:
			sprite.flip_h = true
		else:
			sprite.flip_h = false

Alright some progress. movement seems to be working although now it chases the player regardless of whether or not the player is inside the collision area. This seems to only be an issue when running the script in the start. it fixes itself after manually entering and exiting the collision area

I didn’t noticed that player have to stop lol
Just add “velocity.x = 0” here

func _physics_process(delta):
	velocity.y += gravity * delta
	move_and_slide()
	if chase == true:
		position += (player.position - position)/speed
		animation.play("Jump")
		if(player.position - position) > 0:
			$AnimatedSprite2D.flip_h = true
		else:
			$AnimatedSprite2D.flip_h = false
	else:
			animation.play("Idle")
                        velocity.x = 0            # HERE!!11!!!!11!!

hahah oh yeah i added that after. no worries :slight_smile: also added

var direction = (player.position - global_position).normalized()

for movement but now when running the script the enemy chases the player before entering the collision shape. Might be because I messed up somewhere along the lines

The new script looks like this

extends CharacterBody2D

var speed = 50
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
var chase = false

#drag your animation node here
@onready var animation = get_node("AnimatedSprite2D")

#drag your player node here
@onready var player = get_node("../../Player/Player")

#drag your enemy's sprite node here
@onready var sprite = get_node(".")

#Then create one Area2d, which will cover area you want your enemy see player

func _on_playerdetection_body_entered(_body):
	chase = true

func _on_playerdetection_body_exited(_body):
	chase = false

#Then check if it's chasing player

func _physics_process(delta):
	velocity.y += gravity * delta
	move_and_slide()
	if chase == true:
		var direction = (player.position - global_position).normalized()
		velocity.x = direction.x * speed
		if direction.length() < 0:
			get_node("AnimatedSprite2D").flip_h = true
		else:
			get_node("AnimatedSprite2D").flip_h = false
	else:
			animation.play("Idle")
			velocity.x = 0
			

It might be because it didn’t said here what body is entering the collision shape

func _on_playerdetection_body_entered(_body):
	chase = true

You’ll have to add player to group and write

func _on_playerdetection_body_entered(_body):
       if _body.is_in_group("group_name"):
	         chase = true

Like this?

I have basically little to no experience with groups

Yeah
Now you have to write

if _body.is_in_group("Player_body"):

YES it works but now the script won’t flip. The enemy just faces right now

Ah, it’s fixed now.
Changed

if direction.length < 0:
to

if direction.x < 0:

Thank you so much I’ve been stuck on this for far too long. You were a massive help