0 votes

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.

in Engine by (55 points)

1 Answer

0 votes
Best answer

you normalize the move vector after having speed added to it, that makes the speed uselss, add speed only at the moveandslide 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
by (306 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.