Godot 4 tutorials on interactions?

Hey, I know what you mean.

My template project COGITO basically works completely on this principle, so it might be of interest to you on how I’ve set this up: COGITO - First Person Immersive Sim Template for GODOT 4

That aside, the most basic implementation would something like this:
Inside or close to your player fps camera, create a
RayCast3D node that lines up with your camera direction. Make sure the Raycast gets transformed with your camera.
Also add a Control Node like a Label so you can display the interaction prompt “Press E to interact”

Then create or add these script parts to your player controller (or separate it out, up to you):

@onready var interaction_raycast : RayCast3D #Make sure the node reference is set correctly
@onready var interaction_label : Label
var interaction_is_reset : bool = true

# Checks if Raycast3D hits something to interact with:
func _process(_delta):
	if interaction_raycast.is_colliding():
		var interactable = interaction_raycast.get_collider()
		is_reset = false
		if interactable != null and interactable.has_method("interact"):
			interaction_label.text = "Press E to interact"
		else:
			interaction_label.text = ""

	else:
		if !is_reset:
			interaction_label.text = ""
			is_reset = true

func _input(event):
	if event.is_action_pressed("interact"): #Adjust to match your InputMap
		if interaction_raycast.is_colliding():
			var interactable = interaction_raycast.get_collider()
			if interactable.has_method("interact"):
				interactable.interact(self) # Calls the interact function on what the Raycast is colliding with, passing self.

This then calls the interact function of the script that’s attached to the collision object the raycast is hitting Make sure that the Raycast3D and any object you want to detect has the correct collision layers/mask set up.

4 Likes