@export var enemy: CharacterBody2D @export var move_speed := 300.0
var player: CharacterBody2D
func Enter():
player = get_tree().get_first_node_in_group(“Player”)
func Physics_Update(delta: float):
var direction = player.global_position - enemy.global_position
if direction.length() > 25:
print("chilling")
enemy.velocity = direction.normalized() * move_speed
else:
enemy.velocity = Vector2()
if direction.length() > 50:
print("idle?")
Transitioned.emit(self, "EnemyIdle")
code in the state that is not transitioning into
extends Node
@export var initial_state : State
var current_state : State
var states : Dictionary = {}
func _ready():
for child in get_children():
if child is State:
states[child.name.to_lower()] = child
child.Transitioned.connect(on_child_transition)
if initial_state:
initial_state.Enter()
current_state = initial_state
func _process(delta):
print(current_state)
if current_state:
current_state.Update(delta)
func _physics_process(delta):
if current_state:
current_state.Physics_Update(delta)
func on_child_transition(state, new_state_name):
if state != current_state:
return
var new_state = states.get(new_state_name.to_lower())
if !new_state:
return
if current_state:
current_state.Exit()
new_state.Enter()
current_state = new_state
code of state machine
Question
one state is working and transitioning but this at the very top is not being transitioned from a state of idle. it is supposed to go into that from the state idle
So, if I understood correctly, your problem is that you have “Idle” and “Follow” states and when direction.length() > 50 your state is not transitioning to “Idle” again? Or you have “Idle” state and it isn’t transitioning to “Follow”? If so, then could you also send your “Idle” state script?
Also, if it’s possible, could you use ~~~ on your whole code, please? It would help a lot
extends State
class_name EnemyIdle
@export var enemy: CharacterBody2D
@export var move_speed := 80.0
var player: CharacterBody2D
var move_direction : Vector2
var wander_time : float
func randomize_Wander():
move_direction = Vector2(randf_range(-1,1), randf_range(-1,1)).normalized()
wander_time = randf_range(1,3)
func Enter():
player = get_tree().get_first_node_in_group("Player")
randomize_Wander()
func Update(delta: float):
if wander_time > 0:
wander_time -= delta
else:
randomize_Wander()
func Physics_Update(delta: float):
if enemy:
enemy.velocity = move_direction * move_speed
#the code for detecting the distance between the player and the enemy through the use of global position
var direction = player.global_position - enemy.global_position
if direction.length() < 30:
print("follo?")
Transitioned.emit(self, "EnemyFollow")
#func _on_detect_body_entered(player):
#Transitioned.emit(self, "EnemyFollow")
this is the code for idle
the problem is that It doesn’t transition to the follow state from the idle
So, as far as I can see, you watched this tutorial, right?
I want to ask if you added your states as child for State machine and assigned your state here
And your enemy here?
Also, could you send your enemy tree, like this
yes i did put move and slide
each state works fine its the transition which is the problem. did the test states work. the enemy sprite is quite big but should not be a problem with the code