I'm not sure I understand the problem, so I will describe one way to make an interaction happen.
First I'll make a few assumptions, try to imagine it or make a demo project:
- You have a player object with a RayCast2D pointing in front of it. (Can also be an Area2D, but i believe a RayCast makes the explanation easier)
- You have a physics object you want to interact with, for example, a StaticBody2D.
- The interaction will be triggered by the Player pressing a key in the keyboard.
- The Interact
key press is processed by the player in the _process
or _physics_process
methods with if Input.is_action_pressed("ui_interact")
Now to make the interaction itself happen:
- The player is in front of the object we want to interact it. And in range of the RayCast2D, so the collision is happening.
- In this case we get the colliding object from the RayCast2D, check if the object is the correct one by checking if it implements a functon or is in a group and call a function from it to tell it that the interaction is happening and it should do something.
OR
- We show a pop up in the screen
OR
- We cause our player to change color... the possibilities are endless!
As for code, our player code could look something like this:
func _physics_process(delta):
if get_node("RayCast2D").is_colliding():
var object = get_node("RayCast2D").get_collider()
if object.is_in_group("Interactable") && Input.is_action_pressed('ui_interact'):
object.do_something() #This would be where your inraction occurs