Enemy script doesn't work

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By MightyRat

I got this flying enemy with some problems: sometimes he stops chasing the Player even if he is in the enemy’s range and ,most important, I can’t change his speed! Script:

extends KinematicBody2D

var speed = 200
var move = Vector2.ZERO
var player = null
var follow = false
var is_dead = false
export (int) var life = 1
var awake = false

func _physics_process(delta):
	move = Vector2.ZERO 
	if player != null:
		move = position.direction_to(player.position) * speed 
		move = Vector2.ZERO
		if awake == false && is_dead == false:

	move = move.normalized()
	move = move_and_collide(move)
	if follow == true && is_dead == false:

func _on_Area2D_body_entered(body):
	if body.name == "Player":
		if follow == false:
			awake = true
			yield(get_tree().create_timer(0.8), "timeout")
			follow = true
			awake = false
		player = body

func _on_Area2D_body_exited(body):
	player = null
	follow = false

func dead():
	life -= 1
	if life < 0:
		is_dead = true
		Globals.nemici -= 1
		speed = Vector2(0, 0)
		$CollisionShape2D.call_deferred("set_disabled", true)
		$Area2D2/CollisionShape2D.call_deferred("set_disabled", true)
	$Particles2D.emitting = true
	yield(get_tree().create_timer(0.2), "timeout")
	$Particles2D.emitting = false
func _on_Timer_timeout():

func _on_Area2D2_body_entered(body):
	if body.name == "Player":
		Globals.gameover = 4

When I change the variable “speed” his speed doesn’t change.

:bust_in_silhouette: Reply From: zen3001

you normalize the move vector after having speed added to it, that makes the speed uselss, add speed only at the move_and_slide function to move (and I’d also advice you to multiple it with delta too, that changes the speed based on the framerate so movement isn’t increased or decreased just because the framrate changes).

something like this is what you’d want


or you could do it where the move variable is changed…

move = move.normalized()*speed*delta