The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

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.

Godot version v3.5.1
in Engine by (35 points)

That's a lot of code to parse. Have you tried stepping through the code and adding breakpoints to see how close the program gets to the bit of code that moves the enemy?

1 Answer

0 votes

trying to read through that code will give my a stroke so instead I suggest following this video as I just used it today and it worked with flying colors:

https://www.youtube.com/watch?v=5xoqp9QMKeg

by (107 points)
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.