0 votes

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

in Engine by (58 points)

1 Answer

+1 vote

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:
        _on_area_entered():

 func _on_area_entered():
      #do somethings

i hope this will be helpfull for you.

by (72 points)

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.

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 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 webmaster@godotengine.org with your username.