extends Node2D @export var speed = 50 @onready var ray_cast_right: RayCast2D = $RayCastRight @onready var ray_cast_left: RayCast2D = $RayCastLeft @onready var ray_cast_top_left: RayCast2D = $RayCastTopLeft @onready var ray_cast_top_right: RayCast2D = $RayCastTopRight @onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D var direction = 1 func _process(delta: float) -> void: if direction == 1 and animated_sprite.flip_h == false: animated_sprite.flip_h = true if direction == -1 and animated_sprite.flip_h == true: animated_sprite.flip_h = false func _physics_process(delta: float) -> void: move(delta) if ray_cast_top_left.is_colliding() or ray_cast_top_right.is_colliding(): queue_free() func move(delta: float) -> void: """Keep moving from left to right, changing direction when near something.""" if direction == 1 and ray_cast_right.is_colliding(): direction = -1 animated_sprite.flip_h = true if direction == -1 and ray_cast_left.is_colliding(): direction = 1 animated_sprite.flip_h = false position.x += direction * speed * delta