0 votes

I'm making a horror game and each scene is separated by a door. By that, I mean that each door leads to a scene. What I've been doing for now is just changing the scene when interacting with a door, and then use a Kinematic Body that was already in the new scene to continue playing.
The problem I'm having is that when going back to the previous scene, since I already had a Kinematic Body, the player appears in the beginning of the scene, when I actually want the player to appear in front of the door it just entered through.

I wanted to fix the issue by creating a global script with a variable that controls the player's position and then change its value from the Raycast script (which changes the scene) so it appears in a different position based on what door the Raycast collides with, but I don't know how to make a variable that controls that through another script
I'm changing the position through code because I need to change it when the player is in another scene and I need a global variable to do that. I would also like to keep the layout of different scenes for each map because I feel more comfortable working that way.

Here's part of the Raycast script which changes the scene when colliding with a specific door, and the part of the player's script which controls the movement.
Raycast script:

if obj.get_name() == "Scene3Door1Area" and Input.is_action_just_pressed("Interact"):
                    get_node("../../../../Control/FadeInOut/AnimationPlayer").play("FadeOut") #Fade out
                    yield(get_tree().create_timer(1), "timeout")
                    get_tree().change_scene("res://Scenes/Game levels and scenes/Game_2.tscn")
                    #get_node("../../..").translate(Vector3(20.229, -2.943, -124.483)) (I want to move the player to this position when it's in the other scene)

Player movement script:

# Walking
dir = Vector3()
var cam_xform = camera.get_global_transform()
var input_movement_vector = Vector2()
if Input.is_action_pressed("movement_forward"):
    input_movement_vector.y += 1
    if $Walking.is_playing() == false:
if Input.is_action_just_released("movement_forward"):
if Input.is_action_pressed("movement_backward"):
    input_movement_vector.y -= 1
if Input.is_action_pressed("movement_left"):
    input_movement_vector.x -= 1
if Input.is_action_pressed("movement_right"):
    input_movement_vector.x += 1

Any help would be appreciated

Godot version v 3.4.2
in Engine by (35 points)

1 Answer

+1 vote
Best answer

There is a node called a position node which can help you with this. If you put a position node where the door is you can set this as the starting position when a player goes back through the door.

You will need to pass some information back to the previous scene to know that you want to start at the same position as the position node. As an example you could do this through an autoload script. In the autoload you can have some boolean variables for each door position and when a player goes through a door set the given boolean to true in the global script and if its true you set the players position when the scene loads to the position2d nodes position.

by (3,009 points)
selected by

Thank you so much, that was much easier than I expected

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.