Hello everyone. I followed this tutorial to make a pathfinding enemy. However, the enemy doesn't move and I have no idea why. The whole thing works with functionstatemachines. This is the code from the character original scene:
extends KinematicBody2D
class_name Character, "res://assets/sprites/dicesword_test_01.png"
const FRICTION: float = 0.15
export (int) var acceleration: int = 40
export (int) var max_speed: int = 100
var mov_direction: Vector2 = Vector2.ZERO
var velocity: Vector2 = Vector2.ZERO
onready var animated_sprite: AnimatedSprite
func _physics_process(_delta: float) -> void:
velocity = move_and_slide(velocity)
velocity = lerp(velocity, Vector2.ZERO, FRICTION)
func move() -> void:
mov_direction = mov_direction.normalized()
velocity += mov_direction * acceleration
velocity = velocity.clamped(max_speed)
Here's the code from the Enemy class:
extends Character
class_name Enemy
var path: PoolVector2Array
onready var navigation: Navigation2D = get_tree().current_scene.get_node("Navigation2D")
onready var player: KinematicBody2D = get_tree().current_scene.get_node("res://Player.tscn")
func chase() -> void:
if path:
var vector_to_next_point: Vector2 = path[0] - global_position
var distance_to_next_point: float = vector_to_next_point.length()
if distance_to_next_point < 1:
path.remove(0)
if not path:
return
mov_direction = vector_to_next_point
if vector_to_next_point.x > 0 and animated_sprite.flip_h:
animated_sprite.flip_h = false
elif vector_to_next_point.x < 0 and not animated_sprite.flip_h:
animated_sprite.flip_h = true
func _on_PathTimer_timeout() -> void:
path = navigation.get_simple_path(global_position, player.position)
Here's the enemy FunctionStateMachine:
extends FiniteStateMachine
func _init() -> void:
_add_state("chase")
func _ready() -> void:
set_state(states.chase)
func _state_logic(_delta: float) -> void:
if state == states.chase:
parent.chase()
parent.move()
func _get_transition() -> int:
return -1
func _enter_state(_previous_state: int, new_state: int) -> void:
match new_state:
states.chase:
animation_player.play("walk1")
And finally, here's the code from the FiniteStateMachine:
extends Node
class_name FiniteStateMachine
var states: Dictionary = {}
var previous_state: int = -1
var state: int = -1 setget set_state
onready var parent: Character = get_parent()
onready var animation_player: AnimationPlayer = parent.get_node("AnimationPlayer")
func _physics_process(delta: float) -> void:
if state != -1:
_state_logic(delta)
var transition: int = _get_transition()
if transition != -1:
set_state(transition)
func _state_logic(_delta: float) -> void:
pass
func _get_transition() -> int:
return -1
func _add_state(new_state: String) -> void:
states[new_state] = states.size()
func set_state(new_state: int) -> void:
_exit_state(state)
previous_state = state
state = new_state
_enter_state(previous_state, state)
func _enter_state(_previous_state: int, _new_state: int) -> void:
pass
func _exit_state(_state_exited: int) -> void:
pass
Please someone help me, I know its a lot but I'm new and in pain.