0 votes

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)
Godot version 4.0.alpha4.official
in Engine by (159 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.