Walking Simulator: How to Build a Godot Interaction System

A Godot interaction system lets players do more than walk through a scene: they can look at an object, read a prompt, and press E to act. Drawing on Zenva’s experience helping over 1,000,000 learners and developers gain digital skills, this tutorial guides you through that connection in a walking simulator, from a reusable interactable class to a lantern you can switch on and off.

This tutorial assumes you know basic Godot and GDScript and have a 3D project with a player controller, camera, the existing interact action bound to E, and the forest scene and lantern assets used below.

Download Project Files

The files and full project are available through the course included in Godot Open World Academy. Choose that guided path if you want the complete project, or keep reading to follow the interaction system here.

Make a Complete Card Battler in Godot e1788415191322 - Walking Simulator: How to Build a Godot Interaction System
FREE GODOT COURSE
LEARN GODOT, UNITY, UNREAL & MORE
ACCESS FOR FREE
AVAILABLE FOR A LIMITED TIME ONLY

Build the Godot Interaction System

How the Interaction System Will Work

Interactions help a player feel connected to the world. For example, an interaction might let them inspect clues, discover secrets, read a character’s thoughts, or toggle a light. These actions can share the same detection system even though each object responds differently.

The system uses a raycast from the center of the player’s camera in the direction they are looking. You can think of this as a laser beam with a limited range, such as a meter and a half. When it hits an interactable within range, you will display a prompt such as “Press E to interact,” “Turn On,” or “Inspect.” Pressing the interaction button will then trigger that object’s behavior.

A hand-drawn interaction diagram shows a ray traveling from the player's camera toward an interactable object.

Start with the interactable scripts, then build the controller that casts the ray and handles the player’s input.

Create the Base Interactable Class

In the project’s Scripts folder, create an Interaction folder, then create interactable.gd inside it. Give the script the class name Interactable and make it extend Area3D:

@abstract
class_name Interactable
extends Area3D

An interactable needs a collision shape so the interaction system can detect it. Using Area3D gives your base class the type of collision object you will use for this detection. The class_name declaration lets other scripts refer to Interactable by name.

The @abstract annotation makes this an abstract class. You cannot attach this script directly to an Area3D node. Instead, it acts as a template for concrete classes, such as a toggle interactable or an information interactable.

The controller can work with the shared Interactable type without knowing whether an object toggles a light or displays information. It calls the same functions, and each concrete class supplies its own behavior.

Define the Abstract Functions

Add three abstract functions to interactable.gd:

@abstract
func get_prompt () -> String

@abstract
func can_interact () -> bool

@abstract
func interact ()

Abstract functions declare a name, any parameters, and any return type, but do not include a function body. Unlike a normal function, you do not add a colon or indented implementation here.

  • get_prompt() returns the text you want to display when the player targets the object, such as “Turn On,” “Turn Off,” or “Inspect.”
  • can_interact() returns whether the object currently allows interaction. For example, a waypoint that should only activate once could return false after its first use.
  • interact() performs the object’s action, such as toggling something or displaying information.

These functions establish the shared interface. The base class does not implement the actions itself.

Create a Concrete ToggleInteractable

Create toggle_interactable.gd in the same Scripts/Interaction folder. Give it the class name ToggleInteractable and make it extend Interactable.

Godot reports errors if a concrete subclass does not implement its inherited abstract functions. To start, copy the three function declarations, remove their @abstract annotations, and give them simple placeholder bodies:

class_name ToggleInteractable
extends Interactable

func get_prompt () -> String:
    return ""

func can_interact () -> bool:
    return true

func interact ():
    pass

For now, the prompt is an empty string, interaction is always allowed, and pass does nothing. These implementations satisfy the abstract function requirements and remove the errors. You will replace the placeholders with your toggle behavior.

Because ToggleInteractable extends Interactable, the future controller can call interact() through the shared type and run this subclass’s implementation.

Add the Toggle Settings

Above the functions in toggle_interactable.gd, add these exported variables:

@export var node_to_toggle : Node3D
@export var start_state : bool = false
@export var toggle_on_prompt : String = "Turn On"
@export var toggle_off_prompt : String = "Turn Off"

The node_to_toggle variable holds the Node3D whose visibility you want to change, such as a light. The start_state Boolean controls its initial visibility. You use false so the target starts off by default.

The two prompt strings describe the action the player can take next. Exporting these settings makes them configurable in the Inspector for different interactable objects.

Apply the Starting State

Add _ready() to set the target’s visibility when the node enters the scene tree:

func _ready ():
    node_to_toggle.visible = start_state

With the default start_state of false, the target begins hidden. Setting the property to true instead makes it begin visible. In this implementation, turning the target on or off means changing its visible property.

Return the Appropriate Prompt

Replace the empty-string implementation of get_prompt() with a check of the target’s current visibility:

func get_prompt () -> String:
    if node_to_toggle.visible:
        return toggle_off_prompt
    else:
        return toggle_on_prompt

If the target is currently visible, the next interaction will turn it off, so you return toggle_off_prompt. If it is hidden, the next interaction will turn it on, so you return toggle_on_prompt. The prompt describes the available action rather than simply naming the current state.

Allow Repeated Interactions

Keep can_interact() returning true:

func can_interact () -> bool:
    return true

Unlike a one-time waypoint, a toggle should remain available so the player can switch the target on and off repeatedly.

Toggle the Target’s Visibility

Finally, replace pass in interact() with the visibility toggle:

func interact ():
    node_to_toggle.visible = !node_to_toggle.visible

The ! operator inverts a Boolean value. If visible is true, it becomes false. If it is false, it becomes true. Each interaction therefore switches the target to the opposite state, and subsequent calls to get_prompt() return the corresponding action text.

With the abstract interface and concrete toggle in place, you can now build the controller that detects and triggers interactables.

Build the Raycast Interaction Controller

Create a Camera-Mounted Raycast

Open the player controller in your scene and add a RayCast3D node as a child of Camera3D. Rename it InteractionController. Parenting it to the camera makes the ray follow the camera’s position and rotation, so detection happens in the direction you are looking.

A raycast checks along a line in a specified direction and can report the collider it hits. By default, the ray points downward. In the Inspector, change Target Position to X = 0, Y = 0, and Z = -1 to point one meter forward. Then set Z = -1.5 to extend the interaction range to 1.5 meters. Godot’s camera forward direction is along its local negative Z axis.

RayCast3D Inspector with Target Position set to X 0, Y 0, and Z -1.5 for a forward interaction ray.

Set Up the Interaction Prompt Label

Add a CanvasLayer child to InteractionController, then add a Label child to the canvas layer. Rename the label InteractPromptLabel. The canvas layer renders its Control-node children as screen-space UI over your 3D world.

With the label selected, open Layout in the Inspector and set Anchors Preset to Center. Resize the label to give the prompt enough space. Enter [E] Interact Prompt as temporary text, with [E] indicating the key you will use to interact.

Set both Horizontal Alignment and Vertical Alignment to Center. Create a new LabelSettings resource, expand its Font settings, and increase the font size to approximately 25 px.

InteractPromptLabel settings with the placeholder text, a 25-pixel font, and centered horizontal and vertical alignment.

Run the scene to check the prompt’s placement and readability. To make it stand out against bright scenery, expand Shadow in the LabelSettings resource and adjust the shadow color’s opacity to add a subtle dark backdrop. Run the scene again to check the result.

The forest game running with the placeholder text [E] Interact Prompt centered over the scene.

For now, this is a static placeholder. After testing detection, you will make the text respond to the current interactable.

Attach the Interaction Controller Script

Select InteractionController and attach a new script named interaction_controller.gd, saved in res://Scripts/Interaction/. The script must extend RayCast3D so you can access the raycast’s built-in functions.

extends RayCast3D

var current_interactable : Interactable
@onready var prompt_label : Label = $CanvasLayer/InteractPromptLabel

The current_interactable variable stores the object you can currently interact with. Its type is the abstract base class Interactable, not a specific subclass. This lets the controller use the shared interface rather than depend on the toggle implementation.

The @onready variable references your prompt label once the node is ready. You can drag InteractPromptLabel from the Scene tree into the script to insert its node path. You are preparing this reference now, but you will use it to update the prompt later.

Detect an Eligible Interactable Each Frame

Inside _process(), retrieve the collider detected by your raycast using get_collider(). You cannot immediately assign this result to current_interactable, because the ray could hit ordinary scenery such as a rock, a tree, or the ground.

First, check that the result is not null and is an Interactable. Then call its can_interact() method. Only if that method returns true should you make it your current target.

func _process (delta : float):
    var hit = get_collider()

    if hit != null and hit is Interactable:
        if hit.can_interact():
            current_interactable = hit

This initial version detects a valid object, but it leaves a problem: once you look away, the conditions no longer pass, yet current_interactable still holds the previous object. You need to clear the old target before checking for a new one each frame.

Clear Stale Targets and Add Debug Output

Set current_interactable to null immediately after retrieving the collider. The controller then starts each frame with no selected object and assigns one only when the ray hits an eligible interactable. If you look at empty space, ordinary scenery, or an interactable that cannot currently be used, the target remains null.

Add print(current_interactable) at the end of the function to verify your detection in Godot’s Output panel. Your script at this stage is:

extends RayCast3D

var current_interactable : Interactable
@onready var prompt_label : Label = $CanvasLayer/InteractPromptLabel

func _process (delta : float):
    var hit = get_collider()
    current_interactable = null

    if hit != null and hit is Interactable:
        if hit.can_interact():
            current_interactable = hit

    print(current_interactable)

The print statement is temporary debugging code. Because it runs every frame, you will see repeated values while testing.

Create a Test Toggle Interactable

In the world scene, create a new ToggleInteractable node using the class you created above. Add a CollisionShape3D child and give it a SphereShape3D. Move the object to a location you can reach with the player.

A collision shape alone is difficult to locate during gameplay, so add a MeshInstance3D child with a SphereMesh. Match the visible sphere to the collider’s bounds.

Select ToggleInteractable and assign the sphere’s MeshInstance3D to Node To Toggle. Leave the prompts as Turn On and Turn Off, and enable Start State so the sphere starts visible.

ToggleInteractable settings with MeshInstance3D assigned as Node To Toggle, Start State enabled, and Turn On and Turn Off prompts.

Enable Area Detection and Verify the Controller

Run the scene and approach the sphere. At first, the Output panel continues to print null, even when you look directly at the object. The issue is a raycast setting, not your target-selection logic.

Your Interactable base class extends Area3D. By default, the raycast does not detect areas. Select InteractionController, expand Collide With in the Inspector, and enable Areas. This allows the ray to detect the interactable’s Area3D collision shape.

Run the scene again, move within the ray’s 1.5-meter range, and look at the sphere. The Output panel should now print a ToggleInteractable reference. Look away and it should return to null, confirming that you both detect valid targets and clear them when they are no longer under the ray.

The player looking at the test sphere while the Output panel prints ToggleInteractable references. The RayCast3D Inspector has Collide With Areas enabled.

Detection is now working, but the label still displays placeholder text and pressing E does not yet trigger an interaction. Next, connect the prompt and input to the current target.

Add Interaction Prompts and a Toggleable Lantern

Update the Interaction Prompt

Open interaction_controller.gd and add an _update_prompt_label() function. Use the current_interactable and prompt_label references you prepared earlier: display the current target’s prompt, or clear the label with an empty string when there is no valid target.

func _update_prompt_label ():
    if current_interactable != null:
        prompt_label.text = "[E] %s" % current_interactable.get_prompt()
    else:
        prompt_label.text = ""

The text "[E] %s" is a string template. The %s placeholder is replaced by the value provided after the % operator, which is the result of current_interactable.get_prompt(). For example, a prompt of "Turn Off" produces "[E] Turn Off". This combines the key hint with the action provided by the interactable.

Inside _process(), call _update_prompt_label() after the code that determines your current target. You can also remove the old print(current_interactable) debugging line.

func _process (delta : float):
    var hit = get_collider()
    current_interactable = null

    if hit != null and hit is Interactable:
        if hit.can_interact():
            current_interactable = hit

    _update_prompt_label()

Each frame, you reset the target and check what the raycast is hitting. Only an interactable that allows interaction becomes the current target. Updating the label afterward means the prompt disappears when you look away and reappears when you look at a valid target.

Run the scene and look at the test sphere. You should see [E] Turn Off. Looking away should leave the label empty.

The test sphere displays the [E] Turn Off interaction prompt in the running forest scene.

Trigger an Interaction With Input

Next, you will handle the existing interact input action. Directly after _update_prompt_label() in _process(), add:

if Input.is_action_just_pressed("interact") and current_interactable != null:
    current_interactable.interact()

Input.is_action_just_pressed() checks whether the action was pressed during the current frame. This triggers the interaction once per press, rather than repeatedly while the key is held. The additional null check prevents you from calling interact() when there is no valid target.

Run the scene again, look at the sphere, and press E. The sphere should disappear. Pressing E again should make it visible. Its interaction area remains available even when the mesh is hidden, so you can still target it. Because you refresh the prompt each frame, the text changes between Turn On and Turn Off as the state changes.

Replace the Test Sphere With a Lantern

The sphere proves the toggle system works. Now apply it to a lantern in the forest environment. Stop the game and delete the MeshInstance3D child of ToggleInteractable. Keep the interactable and its collision shape, which define the target you can interact with.

In the Models/Props folder, locate the Lantern_Wall model and drag it into the scene. Position it beside a tree. Initially, keep the lantern model separate from ToggleInteractable, since the interactable is only your trigger area, not the visible object you want to hide.

Select the interactable’s CollisionShape3D child. Increase its debug color opacity to full opacity and raise its intensity if needed so the shape is easier to see in the editor. Move the toggle interactable so its collision shape surrounds the lantern. This defines the area your interaction ray can target.

The interaction collision shape surrounds the lantern model beside a tree in the Godot 3D viewport.

Add and Configure the Lantern Light

Create a Node3D named Lantern and move it to the lantern’s location. Make the lantern model a child of this node, then add an OmniLight3D as another child. Unlike a directional light, an omni light emits light in all directions from its position. Move it into the center of the lantern.

Expand the light’s Light settings in the Inspector. You will use a warm, flame-like color and adjust the following values:

  • Color: choose a warm yellow-orange color.
  • Energy: set it to approximately 3 to strengthen the light.
  • Indirect Energy: leave the existing value unchanged.
  • Volumetric Fog Energy: set it to approximately 5 to increase the light’s contribution to the haze.
  • Size: increase it to approximately 0.5 m, giving the light source a wider appearance.

The stronger volumetric fog contribution makes the light appear hazier. Together with the bloom already configured in the project’s environment, this produces a glow around the lantern.

Godot's OmniLight3D Inspector shows a warm color, Energy 3, Volumetric Fog Energy 5, and Size 0.5 m beside the glowing lantern.

Connect the Light to the Toggle Interactable

Select ToggleInteractable and drag OmniLight3D into its Node to Toggle property. Turn Start State off so the lantern begins with its light disabled. You are toggling the light node, not the model, so the lantern itself stays visible when the light is off.

Reparent ToggleInteractable under Lantern as well. Your parent now groups the lantern model, the light, and the interaction trigger, with CollisionShape3D remaining beneath the interactable. This lets you move the complete lantern setup by moving a single parent node.

The Lantern parent groups the lantern model, OmniLight3D, and ToggleInteractable, with CollisionShape3D beneath the interactable.

Test the Finished Lantern

Run the scene and approach the lantern. Its light should start off, and looking at it should display [E] Turn On. Press E to enable the light, then press it again to disable it. The prompt should follow the current state and disappear when you look away.

The unlit lantern remains visible in the running scene with the [E] Turn On prompt.

When the light changes state, the volumetric haze takes a short time to settle, creating a visible fade-in and fade-out effect. Your controller now connects a contextual prompt and input-driven interaction to a lantern in the scene.

Your Interaction System Is Ready

You now have a working lantern interaction, from detecting the target to updating the prompt and toggling its light. Along the way, you have:

  • Defined an abstract Interactable interface and a concrete visibility toggle.
  • Configured a camera-mounted RayCast3D to detect nearby areas and clear stale targets.
  • Connected contextual prompt text and the interact input action.
  • Grouped the lantern model, light, and interaction trigger into one movable setup.

Keep this separation between detection and object behavior in mind as you continue working with interactables: the controller uses the shared interface, while each object supplies its own response.

For a structured path through complete Godot projects, explore Godot Open World Academy and continue building your open-world game development skills step by step.

Did you come across any errors in this tutorial? Please let us know by completing this form and we’ll look into it!

FREE COURSES
Python Blog Image - Walking Simulator: How to Build a Godot Interaction System

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.