Changing scenes

Hi i am making a 3d game, this is my first game and i am still learning i am very new to coding like this except very basic things in other coding languages, but anyway i want to be able to click (press e) or be up close to something e.g a wall which then teleports to the next scene, i have been stuck on this for ages since theres no definitive answer that isnt too difficult for me to understand, any help would be great but if you are explaining please do step by step what i should do and please be in 3d.
Thank you.

I think you want to make use of Areas.
You want to create an area which acts as a trigger for your scene transition.
I’m assuming you have some kind of player character. This character will most likely be a character body.
The trigger area has a signal called body_entered. If this signal is emitted, you can change the scene.
If you want to change a scene on a button press, you could just check for Input and call the proper scene transition logic.

1 Like

Thank you so much titusio i worked this out

func _on_body_entered(_body):
get_tree().change_scene_to_file(“res://apartment stairway.tscn”)

i have figured it out but i tryed to make it clickable by doing this
func _on_body_entered(_body):
if Input.is_action_just_released(“click”):
get_tree().change_scene_to_file(“res://apartment stairway.tscn”)
but it did not work if i get rid of body entered if you click it works but it works anywhere, any tips?

This doesn’t work because you are kind of betting that the mouse pressed event and the on body entered event happen in the EXACT same frame.

So instead check if the input is pressed on body entered.

func _on_body_entered(_body):
    if Input.is_action_pressed(“click”):
        get_tree().change_scene_to_file(“res://apartment stairway.tscn”)

Note, that the Input check looks at the current state of the input and not if the key was just released.

This only work if the key is already pressed, when the player enters the area. If the players enters the player and then presses the key, it wont work.

1 Like

ah right thanks but this still seems to not work consistently and either way is not what i am trying to do, which is go into the area then click which loads the next scene.

You could change every frame:

func _process(delta):
    if Input.is_action_just_pressed(“click”):
        # check if there are any bodies in the area
        if area.get_overlapping_bodies().size() > 0:
            get_tree().change_scene_to_file(“res://apartment stairway.tscn”)

You might want to rewrite this and avoid the nesting but I’ll leave that up to you

To create a basic teleportation system in your Godot 3D game:

1. Create Teleportation Area:

  • Add a Spatial node with a CollisionShape to represent the teleportation area.

2. Create Destination Scene:

  • Make a new scene for the destination where the player will be teleported.

3. Attach Script:

  • Attach a GDScript to the teleportation area node.

4. Write Teleportation Script:

  • Implement logic to detect player proximity and trigger teleportation on a specific input (e.g., ‘E’).

5. Add Player to Group:

  • Add the player character nodes to a group, e.g., “player.”

6. Teleportation Functions:

  • Implement functions to check if the player is in the teleportation area and to teleport the player to the destination scene.

7. Input Mapping:

  • Set up an input action (e.g., “teleport”) and map it to a key or button in “Project Settings.”

8. Test:

  • Run the game and test teleportation by approaching the area and pressing the assigned key.

Adjust the script to suit your game’s needs.You can check Help - Godot Forum

Thank you