My kinematicbody2d enemy is slow movement while following my player

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

extends KinematicBody2D

onready var player = get_parent().get_node(“Player”)
var move = Vector2(0,0)
var speed = 1

var player_in = false

func _physics_process(delta):
if player_in == true:
move = position.direction_to(player.position) * speed
else:
move = Vector2.ZERO

move = move.normalized()
move = move_and_slide(move)

func _on_Area2D_body_entered(body):
if player.name == “Player”:
player_in = true
print(“im in”)

func _on_Area2D_body_exited(body):
player_in = false
print(“im out”)

:bust_in_silhouette: Reply From: exuin

So your problem is two-fold.

move = position.direction_to(player.position) * speed
...
move = move.normalized()

First off, you have speed equal to 1. This means your enemy moves at 1 pixel per second. Secondly, normalized() sets the length of the vector to 1. This means that even if your speed was higher than 1, it would be set to 1 regardless. You should increase the value of speed and not normalize the movement vector.

var speed = 200 # that looks fast enough

func _physics_process(delta):
    if player_in: # no need to check if it's equal to true since it's a boolean already
        move = position.direction_to(player.position) * speed
    else:
        move = Vector2.ZERO

    move = move_and_slide(move)

thank you very much

juanMAMAW | 2020-09-23 09:37