How to make the player stop outside the radius on the enemy

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

Hi,
I made a script for the player to walk towards the enemy’s position, the problem is I don’t know how to determine the position so that the player can stop outside the enemy’s radius

The script that I made always makes the player stop right at the enemy’s position point. The temporary solution I have is to detect the enemy radius with area2d, but that’s not good

enter image description here

:bust_in_silhouette: Reply From: ramazan
func example():
  var enemy = $enemy.global_position
  if $player.position.distance_to(enemy) < 3:
	  print("bla bla")
  pass

(“distance_to”)
Note : Number 3 ( three ) . Try changing

///////////////////////////
I have a question for you too. How did you add that image here? I couldn’t find where to add

Put the image link (eg from: imgbb.com) that you have uploaded in the green box in the image

enter image description here

David Wong | 2022-01-23 11:25

Thank you, so we can’t add from PC, right?
////
By the way, did you solve the problem?

ramazan | 2022-01-23 11:29

btw thanks for the answer, can you tell me how can i find out the position of the vector outside that radius?

enter image description here

David Wong | 2022-01-23 11:30

No, you can’t upload directly :slight_smile:

David Wong | 2022-01-23 11:31

Does the enemy have a Collisionshape?

func example():
  var enemy = $enemy.global_position
  #Let's say there is.
  #I assume you have a field
  var enemy2 = $Enemy/Area2D/CollisionShape2D.shape.radius
  if $player.position.distance_to(enemy) < enemy2:
          print("bla bla")

  pass

ramazan | 2022-01-23 11:40

Wah great idea, I should have added a collisionShape. thanks

David Wong | 2022-01-23 12:00