Scene transition sometimes does not move character to the correct position

I have the issue that when moving between scenes through doorways, the character is sometimes, but not always not moved to the correct coordinates after the scene change, instead staying at the position where I placed the character in the editor. The same scene change might even randomly work or fail during the same session of play. Currently, the odds of it failing are about 80%, though I get the impression they are lower if I just turned on the computer.

My general scene layout is like this:

  • One character object in every scene
  • One default spawn point in every scene
  • Several doors (invisible trigger colliders) and an identical number of spawn points

The general scene change transition mechanic is as follows:

  • Every door knows which scene it leads to, as well as the name of the spawn point they lead to
  • When the character enters a door, the name of the target spawn point is saved in a singleton, and the target scene is loaded
  • When a scene is loaded, the character stored spawn point name is read from the singleton, and the character is automatically teleported to that spawn point (or the default spawn point if no spawn point of that name could be found)

Here’s the main Classes I use for that:

For the “Doors” (this script is located on a trigger-collider):

[RequireComponent(typeof(Collider))]
public class Teleport_To_New_Scene : MonoBehaviour
{
    [Tooltip("The name of the scene this 'door' leads to.")]
    public string target_scene_name;

    [Tooltip("The name of the spawn point at which the player should emerge in the new scene.")]
    public string target_spawn_point_name;

    void OnTriggerEnter(Collider collider)
    {
        GameObject entering_object = collider.gameObject;
        if (entering_object.tag == Tags.PLAYER)
        {
            Scene_Change_Controller.Instance.target_spawn_point_name =
                target_spawn_point_name;
            Scene_Change_Controller.Instance.character_facing =
                entering_object.transform.rotation.eulerAngles.y;
            SceneManager.LoadScene(target_scene_name);
        }

    }
}

For the Scene Initialisation (this script is located on an empty “Scene_Init” game object):

class Field_Scene_Init : MonoBehaviour
{
    private const string DEFAULT_SPAWN_POINT_NAME = "Default_Spawn_Point";

    void Start()
    {
        Teleport_Player_To_Spawn_Point();
        Set_Player_Rotation();
    }

    private void Teleport_Player_To_Spawn_Point()
    {
        GameObject player = GameObject.FindGameObjectWithTag(Tags.PLAYER);
        GameObject spawn_point = Scene_Change_Controller.Instance.Get_Target_Spawn_Point();
        if(spawn_point == null)
        {
            spawn_point = Game_Object_Finder.Find_Spawn_Point_By_Name(DEFAULT_SPAWN_POINT_NAME);
        }
        player.transform.position = spawn_point.transform.position;
        Debug.Log("player.transform.position: " + player.transform.position);
    }

    private void Set_Player_Rotation()
    {
        GameObject player = GameObject.FindGameObjectWithTag(Tags.PLAYER);
        float target_facing = Scene_Change_Controller.Instance.character_facing;
        player.transform.Rotate(0, target_facing, 0, Space.Self);
    }
}

The Scene Change Controller (this is a singleton)

class Scene_Change_Controller : Singleton<Scene_Change_Controller>
{
    public string target_spawn_point_name = "Default_Spawn_Point";
    public float character_facing = 0f;

    public GameObject Get_Target_Spawn_Point()
    {
        return Game_Object_Finder.Find_Spawn_Point_By_Name(target_spawn_point_name);
    }
}

I have already tried to debug it in various ways.

Maybe my most interesting finding thus far was by adding a log to the very end of the Teleport_Player_To_Spawn_Point-function to read out the player’s coordinates after teleporting. In the log, the correct coordinates are always displayed, regardless of whether the player ends up appearing at the correct coordinates or not.

I’ve also already checked that there is only one player object present in the scene.

Does anyone have any idea why this is happening, and what I can do about it?

I wish I knew. I am running into the same problem. Did you ever get it fixed? @kira_resari

Hi, I know this is old but, I was having this exact same issue.

The solution for me was disabling the CharacterController before changing the player position
and then enabling it after. Also if you use a NavMeshAgent instead, disable it before
changing the player position or use navMeshAgent.Warp(Vector3 newPosition)

Hope this helps, Have a Great Day.