Enemy stuck in place

Godot Version

4.3

Question

so basically im having a problem where my enemy just hangs in place while playing walking animation in there “wander state” instead of going to left bounders and then right bounders while waiting for player here is the script :


@export var player : CharacterBody2D
@export var Speed : int = 50
@export var CHASE_SPEED : int = 100
@export var ACCELERTATION : int = 300
@export var bullet : Node2D

@onready var sprite : AnimatedSprite2D = $AnimatedSprite2D
@onready var Detectionarea : Area2D = $detectionarea
@onready var timer : Timer = $Timer

var GRAVITY : int = 800
var DIRECTION : Vector2
var right_bounds : Vector2
var left_bounds : Vector2
var playerentered : bool = false
var health : int = 120
var can_take_damage : bool = true
var playinghurtanimation : bool = false
enum States{
	WANDER,
	CHASE
}
var current_state = States.WANDER

func _ready():
	left_bounds = self.position + Vector2(-50,-50)
	right_bounds = self.position + Vector2(50,50)


func _physics_process(delta):
	if playinghurtanimation:
		await (get_tree().create_timer(0.6).timeout)
		playinghurtanimation = false
	if self.health < 0:
		queue_free()
	handle_gravity(delta)
	handle_movment(delta)
	look_for_player()
	change_direction()

func look_for_player():
	if playerentered:
		chase_player()
	elif current_state == States.CHASE:
		stop_chase()
	elif current_state == States.CHASE:
		stop_chase()
		
func change_direction():
	if current_state == States.WANDER:
		if sprite.flip_h:
			if self.position.x <= right_bounds.x:
				DIRECTION = Vector2(1,0)
			else:
				sprite.flip_h = false
		else:
			if self.position.x >= left_bounds.x:
				DIRECTION = Vector2(-1,0)
			else:
				sprite.flip_h = true
	else:
		DIRECTION = (player.position - self.position ).normalized()
		DIRECTION = sign(DIRECTION)
		if DIRECTION.x == 1:
			sprite.flip_h = true
		else :
			sprite.flip_h = false
func handle_gravity(delta):
	if not is_on_floor():
		velocity.y = GRAVITY * delta

func handle_movment(delta):
	if not playinghurtanimation:
		if current_state == States.WANDER:
			velocity = velocity.move_toward(DIRECTION * Speed, ACCELERTATION * delta)
			if not playinghurtanimation:
				$AnimatedSprite2D.play("walk")
		else:
			velocity = velocity.move_toward(DIRECTION * CHASE_SPEED , ACCELERTATION * delta)
			if not playinghurtanimation:
				$AnimatedSprite2D.play("attackrun")
	
	move_and_slide()
func chase_player():
	timer.stop()
	current_state = States.CHASE
	
	
func stop_chase():
	if timer.time_left <= 0:
		can_take_damage = true
		timer.start()	
func enemy_take_damage():
	if can_take_damage:
		health -= 20
		can_take_damage = false
	if health <= 0:
		self.queue_free()
func _on_detectionarea_body_entered(body: Node2D) -> void:
	if body == player:
		playerentered = true

func _on_detectionarea_body_exited(body: Node2D) -> void:
	if body == player:
		playerentered = false

func take_damage():
	print(health)
	$AnimatedSprite2D.play("hurt")
	playinghurtanimation = true
	health -= 10
	if health <= 0:
		queue_free()
func imtheenemy():
	pass```

I think you need to change your velocity to velocity = position.move_toward(#other stuff here)

nope that just made it worse…

How can that make it worse? They weren’t moving before. Please elaborate if it changed something on what those changes are, that way it can help understand. If it wasn’t working before and it changed something what exactly did it change?

1 Like

Have you tried simplifying your velocity to confirm that that is your issue?

Just set it to velocity.x = 1 * 100

Then work from there.

You can also use print statements to help debug your code. Print your velocity before and after your code where you change velocity. Also, if you print position before move_and_slide you’ll see if it is moving and for some reason not moving a lot or moving just back and forth really quickly.

These things help with debugging. Simplify problem areas, use print statements. These two things can help tons in simplifying debugging, especially when we can’t physically mess around with your setup to see what is going on.

1 Like