How would you do a proximity check?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By javrocks

When my player gets to close to the enemy i would like the enemy to attack.
I would like to do it with code and not collision shapes

:bust_in_silhouette: Reply From: reshadiye1

Well, i will try to explain how to do.

Firstly we need to learn that we are approaching the enemy.

I think we can do this with:

const MINIMUM_DISTANCE = 300 # minimum 300 pixel

onready var enemyNode = $EnemyNode # enemy node

onready var myNode = $MyNode # our node

func _process(delta):
     if enemyNode.global_position.x - myNode.global_position.x < MINIMUM_DISTANCE:

 func _on_area_entered():
      #do somethings

i hope this will be helpfull for you.

If you want to do it like that, you need to use

abs(enemyNode.global_position.x - myNode.global_position.x) < MINIMUM_DISTANCE

Otherwise, it will only work correctly when the player is to the left of the enemy.

archeron | 2021-03-03 23:03