kinematicbody2D trying to move towards player by vector keeps failing

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By slu
:warning: Old Version Published before Godot 3 was released.

Hi, i have node called “ghost” which should follow the player everywhere, however, it does not.
It just keeps going to the right, even thought it counts well the angle. I suspect there is something broken in the calculation of the final move vector, but since it is simple trigonometry, I believe I setted it up correctly.
I would gladly accept any tips on what might be wrong.
How does it look like: (the godot icon should follow the player)
enter image description here

Here is my ghost code:

extends KinematicBody2D
var reached = false
const SPEED = 1
const ROTATION_SPEED = 20
 
func _ready():
    set_process(true)
    pass

func _process(delta):
    var ang = get_angle_to(get_node("../player").get_pos())
    rotate(ang*ROTATION_SPEED*delta)
    var x = cos(ang)
    var y = -sin(ang)

if !reached:
	move(Vector2(x*SPEED,y*SPEED))


func _on_player_body_enter( body ):
    reached = true

func _on_player_body_exit( body ):
    reached = false

the final vector of the ghost (/ kinematicbody2D) is always 1 on x. how so?

:bust_in_silhouette: Reply From: quijipixel

I see an error there:

func _process(delta):
    var ang = get_angle_to(get_node("../player").get_pos())
    rotate(ang*ROTATION_SPEED*delta)
    var x = cos(ang)
    var y = -sin(ang)

if !reached:
    move(Vector2(x*SPEED,y*SPEED))

The final if is not indented to the func _process, so is not executed every time the process function is called. By the way, an easier way to get the direction the ghost must go is just substracting the players position to the ghosts position, this will give you the ghost directional vector, just remember to normalize it.

var direction =  (get_node("../player").get_pos() - get_pos()).normalized()