Enemy State Machine: calculating distance between player-enemy [2D Platformer]

Godot Version

4.2.2

Question

I have made a state machine to handle two states of the enemy: Wander, Chase.
My problem is that I was learning from a code made for a top view type game and I am dealing with a 2D platformer.
What would be the best way to calculate the distance between player and enemy so I can set a treshold for switching the states?

Simple vector math
var distance_to_enemy = (enemy.global_position - player.global_position).length()

for treshold just to

if distance_to_enemy <= your_treshold:
do_stuff()

1 Like

I need to stop being afraid of vectors, it was indeed quite simple to set up, thank you very much :slight_smile:

1 Like

yeah vectors are really useful when you get your head around them