hello guys, here is a code for enemy’s ai,
extends CharacterBody2D
@export var patrol_points : Node
enum State {Idle, Walk, Die}
var current_state : State
var direction : Vector2 = Vector2.LEFT
var points : int
var positions : Array[Vector2]
var current_point : Vector2
var current_point_position : int
func _ready():
if patrol_points != null:
points = patrol_points.get_children().size()
for point in patrol_points.get_children():
positions.append(point.global_position)
current_point = positions[current_point_position]
current_state = State.Idle
func _physics_process(delta):
enemy_falls(delta)
he_still_stands_but_with_animation(delta)
finally_he_walks_hooray(delta)
move_and_slide()
func enemy_falls(delta):
velocity.y += 1000*delta
func he_still_stands_but_with_animation(delta):
velocity.x = move_toward(velocity.x, 0, 1500*delta)
current_state = State.Idle
func finally_he_walks_hooray(delta):
if abs(position.x -current_point.x) > 0.5:
velocity.x = direction.x*1500*delta
current_state = State.Walk
else:
current_point_position += 1
if current_point_position >= points:
current_point_position = 0
current_point = positions[current_point_position]
if current_point.x > position.x:
direction = Vector2.RIGHT
else:
direction = Vector2.LEFT
my enemy should move between two marker2d points, and if he reaches left point his direction switches to right, and if he reaches right point - his direction switches to left, but my enemy is just goes to default right direction and then falls off the ground, what’s wrong here?