Enemy Node moving away from the player instead of chasing it

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

Hi, I’m stuck on this problem, since I can’t really see what I’m doing wrong.
I hope it’s not a silly thing, here is the code:

extends KinematicBody2D

var velocity = Vector2.ZERO
var chase_speed = 100
var player = null

func _ready():

func _physics_process(delta):
    if player:
	    velocity = (player.position - position).normalized() * chase_speed

func _on_DetectandoPlayerArea_body_entered(body):
	if body.is_in_group("player"):
		player = body
		print("Eu vejo você")

func _on_DetectandoPlayerArea_body_exited(_body):
	player = null

I really hope this hasn’t been answered before, I’m searching for this for hours, and sometimes we just can’t find the right words to search for.
The detection works as planned, pretty simple, but somehow to enemy just keeps drifting away from the player towards the LEFT always.
If I make velocity negative, it drifts towards the RIGHT instead, but never follows the player.

Edit: So I printed the values like this:

print("Velocity: ",velocity,"\n","PosiPlayer: ", player.global_position, "\n", "PosiEnemy: ", global_position)

The results:

Velocity: (-97.145355, -23.722973)
PosiPlayer: (375.452332, -109.84481)
PosiEnemy: (1157.895752, 81.2285)

So again, just chaging velocity by multiplying it to -1 or itself to -, simply changes the position it slides instead of left, it slides right.

The Player Node is on Layer 1, Mask 1.
The enemy is on Layer 3, Mask 1.
Putting them on the same layer does nothing different aswell.

To illustrate here what happens:

Another new edit here, I started a new clean project on the same engine version, I still get the same bug.

Please use global-position instead of position

horsecar123 | 2022-03-07 02:13

But even if I change to:

velocity = global_position.direction_to(player.global_position) * chase_speed

using direction_to, the result is the same.

luiscesjr | 2022-03-07 11:51

:bust_in_silhouette: Reply From: macktns

Use direction_to to calculate the direction

Even if I change to

velocity = position.direction_to(player.position) * chase_speed

It has the same output, the enemy keeps moving to the left*.

luiscesjr | 2022-03-07 11:46

:bust_in_silhouette: Reply From: luiscesjr

So after a long time fuming my brain cells over this, I found out how silly it was of a bug, created by myself and my lack of experience on the editor.

What happened was, even though my player sprite was in the middle of the screen, it’s position in the editor was at the top left corner, and everything I put in the scene that should look for the player was actually looking in that direction.

Now all is well, here is a picture to better illustrate to where it was pointing out, if anybody has the same problem, just make sure to also check this out.
It was alongside my teste node pointer, where it says TesteMundo.

Thanks for everyone who tried helping.