if the opponent comes to the player from above and makes contact with him, he will "stick" to him

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

link to a video demonstrating the problem
ghost.gd

extends "state.gd"

@export_node_path(Area2D) var _area
@export_node_path(AnimatedSprite2D) var _animated_sprite

var area : Area2D
var animated_sprite : AnimatedSprite2D

@export var speed := 150.0

func _ready():
	super()
    area = get_node(_area)
    animated_sprite = get_node(_animated_sprite)

func play_animation(direction: Vector2) -> void:
    match direction:
    	Vector2.ZERO:
			animated_sprite.play("idle")
		Vector2.LEFT:
			animated_sprite.play("move_side")
			animated_sprite.flip_h = true
		Vector2.RIGHT:
			animated_sprite.play("move_side")
			animated_sprite.flip_h = false
		Vector2.UP:
			animated_sprite.play("move_up")
		Vector2.DOWN:
			animated_sprite.play("move_down")

func _physics_process(_delta):
	var bodies := area.get_overlapping_bodies()
	var direction := Vector2.ZERO
	for body in bodies:
		if body is Player:
			direction = ghost.global_position.direction_to(body.global_position)
			direction = Vector2(snapped(direction.x, 1), snapped(direction.y, 1)).normalized()	
	play_animation(direction)
	ghost.velocity = direction * speed
	ghost.move_and_slide()

state.gd

extends Node2D

@export_node_path(CharacterBody2D) var _ghost

var ghost : Ghost

func _ready():
    ghost = get_node(_ghost)