Interact with an item when the character is over it

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Adam
:warning: Old Version Published before Godot 3 was released.

I have a character (a KinematicBody2D with a CollisionShape2D) and I want to perform an action on an item (Area2D with CollisionShape2D) in front of him when he is over that item. The action will be performed by pressing a button. How can I “see” when the character is over that item?

:bust_in_silhouette: Reply From: Vladislav Vorobiev

You can check distance between item and character and if it is lesser than defined amount, enable button that allows to perform an action.


func can_use_item(char, item):
  # char and item are Nodes
  if char.get_global_pos().distance_to(item.get_global_pos()) <= 100:
    return true
  return false

Thank you, this is a very nice way and works really good.

Adam | 2016-07-14 22:03