Godot Version
4.3
Question
so basically im making an enemy and it has 3 states so far hurt and chase and wander all stats work fine expect chase while in chase state the enemy just goes infintly to the right instead of actually chasing the player here is my code (to not get confused check the handle movement function cuz thats prob where the problem is ) :
extends CharacterBody2D
# Exported variables
@export var player: CharacterBody2D
@export var health = 150
# Node references
@onready var sprite: AnimatedSprite2D = $AnimatedSprite2D
@onready var detection_area: Area2D = $DetectionArea
@onready var timer: Timer = $Timer
@onready var timer2: Timer = $Timer2
# Movement variables
var gravity: int = 500
var speed: int = 50
var acceleration: int = 600
var chase_speed: int = 100
var direction: Vector2
var right_bounds: Vector2
var left_bounds: Vector2
var target_velocity
# State management
enum States { WANDER, CHASE, HURT }
var current_state: States = States.WANDER
var player_entered: bool = false
var taking_damage: bool = false
func _ready() -> void:
left_bounds = position + Vector2(-20, 0)
right_bounds = position + Vector2(20, 0)
func _process(delta: float) -> void:
$Label.text = str(health)
print("current state : ",current_state)
if health <= 0:
handle_death()
return
handle_movement(delta)
handle_gravity(delta)
update_direction()
chase_player()
func handle_movement(delta: float) -> void:
if current_state == States.HURT:
return
match current_state:
States.WANDER:
velocity = velocity.move_toward(direction * speed, acceleration * delta)
if not taking_damage:
sprite.play("walk")
States.CHASE:
direction = global_position.direction_to(player.global_position)
velocity = direction * chase_speed
if not taking_damage:
sprite.play("attackrun")
move_and_slide()
func handle_gravity(delta: float) -> void:
if not is_on_floor():
velocity.y += gravity * delta
else:
velocity.y = 0
func update_direction() -> void:
match current_state:
States.WANDER:
if sprite.flip_h:
if position.x >= left_bounds.x:
direction = Vector2(-1, 0)
else:
sprite.flip_h = false
else:
if position.x <= right_bounds.x:
direction = Vector2(1, 0)
else:
sprite.flip_h = true
States.CHASE:
print("player position : ",player.position)
print("position : ", position)
print("direction : ",direction)
direction = global_position.direction_to(player.global_position)
sprite.flip_h = direction.x < 0
func handle_death() -> void:
sprite.play("new_animation")
await get_tree().create_timer(0.8).timeout
queue_free()
func take_damage() -> void:
health -= 10
print("Health:", health)
if health <= 0:
handle_death()
func _on_player_youshouldtakedamagebadenemy() -> void:
current_state = States.HURT
if current_state == States.HURT:
sprite.play("hurt")
$AudioStreamPlayer2D.play()
take_damage()
await get_tree().create_timer(0.5).timeout
current_state = States.WANDER
func _on_timer_timeout() -> void:
taking_damage = false
func _on_detection_area_body_exited(body: Node) -> void:
if body == player:
player_entered = false
func _on_animated_sprite_2d_animation_finished() -> void:
if sprite.animation == "hurt":
current_state = States.WANDER
func chase_player():
if player_entered and current_state != States.HURT:
current_state = States.CHASE
func _on_detectionarea_body_entered(body: CharacterBody2D) -> void:
print("detected the plaayer")
if body == player:
player_entered = true
chase_player()```