Why does my sprite move crazy when i writed a script look at my mouse cursor?

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

i writed a script for the sprite to loo at my mouse cursor but when i play the game it runs away from the cursor and goes crazy. this is the script i wrote:

extends KinematicBody2D

export var speed = 200

var _smoothed_mouse_pos: Vector2

func _process(delta: float) → void:
_smoothed_mouse_pos = lerp(_smoothed_mouse_pos, get_global_mouse_position(), 0.3)

func _physics_process(delta):
var direction = Vector2()
if Input.is_action_pressed(“up”):
direction += Vector2(0, -1)
if Input.is_action_pressed(“down”):
direction += Vector2(0, 1)
if Input.is_action_pressed(“left”):
direction +=Vector2(-1, 0)
if Input.is_action_pressed(“right”):
direction += Vector2(1, 0)

move_and_slide(direction * speed)