I tried to follow heartbest tutorial and i made the enemy chasing system and everything works fine except the speed of the enemy. It detects and moves towards the player. However it moves veerryyy slow. Is there something im doing wrong in the script. And it only seems to happen in GODOT 4
extends CharacterBody2D
const EnemyDeathEffect = preload("res://scenes/enemy_death_effect.tscn")
@export var ACCELERATION = 300
@export var MAX_SPEED = 50
@export var FRICTION = 200
enum {
IDLE,
WANDER,
CHASE
}
var state = CHASE
var knockback = Vector2.ZERO
@onready var sprite = $AnimatedSprite
@onready var stats = $Stats
@onready var playerDetectionZone = $PlayerDetectionZone
func _ready():
print(stats.health)
func _physics_process(delta):
knockback = knockback.move_toward(Vector2.ZERO, FRICTION * delta)
set_velocity(knockback)
move_and_slide()
knockback = velocity
match state:
IDLE:
velocity = velocity.move_toward(Vector2.ZERO, 200 * delta)
seek_player()
WANDER:
pass
CHASE:
var player = playerDetectionZone.player #my player is called playerg
if player != null:
var direction = (player.global_position - global_position)/MAX_SPEED #.normalized()
velocity = velocity.move_toward(direction * MAX_SPEED, ACCELERATION * delta)
sprite.flip_h = velocity.x < 0
move_and_slide()
func seek_player():
if playerDetectionZone.can_see_player():
state = CHASE
func _on_hurtbox_area_entered(area):
stats.health -= area.Damage
knockback = area.knockback_vector * 120
#knockback = area.knockback_vector * 150
func _on_stats_no_health():
queue_free()
var enemyDeathEffect = EnemyDeathEffect.instantiate()
get_parent().add_child(enemyDeathEffect)
enemyDeathEffect.global_position = global_position
enemyDeathEffect.offset.y = -15