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.
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.
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.
thanks
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