Godot Version
Godot Ver. 4.3
Question
Hello
I was trying to make a Following Player state using detection area. when I’m getting position for player that inside the area using global_position, the errors occurred.
why is that?
here’s the following state code
extends State
class_name FollowState
@export var enemy : CharacterBody3D
@export var move_speed := 1
var player = null
var chase = false
func Enter():
chase = true
func Physics_Update(delta: float):
if chase:
var direction = player.global_position - enemy.global_position
enemy.velocity = direction.normalized() * move_speed
else:
chase = false
func _on_detection_area_body_entered(body: Node3D) -> void:
player = body
chase = true
here’s my Finite State Machine code
class_name FiniteStateMachine
extends Node
@export var initial_state = CharacterBody3D
var current_state = State
var states : Dictionary = {}
func _ready() -> void:
for child in get_children():
if child is State:
states[child.name] = child
child.Transitioned.connect(on_child_transition)
if initial_state:
initial_state.Enter()
current_state = initial_state
func _process(delta: float) -> void:
if current_state:
current_state.Update(delta)
func _physics_process(delta: float) -> void:
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()