Do you know any step by step tutorial on how to add interactions in an 3d FPS style game? You know, the ones with “press E to interact” and possibly Raycast. So far the guides I checked were either outdated or not what I was looking for. Thanks in advance.
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.
ok, I’ve pasted the code in the player control node, it says: Cannot call method “is_collliding” on a null value. I’ve downloaded the Github project you mentioned and it doesn’t really help, besides the fact that crashes I can’t really navigate such big project.
The InteractionComponent Node3D should contain the script checking for interactable objects.
The node references should be set correctly (especially the Raycast3D)
The Raycast3D should be parented in a way that it moves with the camera.
The Raycast3D should be set to check on a different layer then Layer 1, as it would otherwise collide with the Character collission shape. In my example I simply set it to 2.
The Interactable Object needs to be on the same collision layer as the Raycast3D to be detected (in my example it’s set to 1 and 2, so the Raycast3D picks it up AND player can’t clip through it)
I managed the interaction_is_reset thing, and the layers since everything is on layer 1 by default, but still it gives me the same error. Also where does the InteractionComponent comes from? Why is StaticBody3D an interactable object? It says: “This node has no shape, so it can’t collide or interact with other objects”
I’m trying to follow this tutorial but so far no luck: https://www.youtube.com/watch?v=adsQFchiOmw
This is just a generic Node3D that I have my script attached to (I like to have it separate from the CharacterBody3D).
Why is StaticBody3D an interactable object?
It’s not, at least not explicitly. I created a simple interaction script that has the “interact” function. Technically any object that has a collision shape can be made interact-able with this method.
In my case, I created a StaticBody3D and attached a MeshInstance and a CollisionShape3D to it.
It says: “This node has no shape, so it can’t collide or interact with other objects”
This means your Node3D is missing a collision shape. I’d recommend reading the documentation on how meshes and collisions work in 3D.
I’m losing my mind. I’ve found this guide and the very first slide, Setting up the raycast, gives me three errors: Unexpected identifier in class body.