Unity 2D platform Player position scene transition

I am making a platform game in unity. I am quite new to scripting like 1-2 years now. I usually can read the code and understand it with and sometimes without tutor too. But basically I am not good at writing a scripts. (Partially because of my english.)

I have second scene, lets call it “A”, which after a main menu is a starting scene, where you wake up on a certain position. Waking up is a part of timeline. Timeline have to play at starting point (position), which get destroyed after timeline was once played in actuall build. After that player goes to a different position for scene transition. He transit to other scene “B” on particular position (this part of scripting works) and this scene “B” have two exits. “B” have exit that leads to “A” (starting scene) and “C”. When transiting to “C” everything is alright cause I don’t have to set some special transform point here. I am spawned behind the door, as my player position is set there. If I go from “C” to “B” it use correct exit position. But when I go back to “A” I am spawned at the starting point, where timeline has happened and not near the door.

I used Chat gpt 3.5 to help me. I’m gonna share the scripts I have now.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class OpenDoor : MonoBehaviour
{
    private bool playerDetected;

    public Transform doorPos;
    public float width;
    public float height;

    public LayerMask whatIsPlayer;

    [SerializeField]
    private string sceneName;

    SceneSwitch sceneSwitch;

    private void Start()
    {
        sceneSwitch = FindObjectOfType<SceneSwitch>();
    }

    private void Update()
    {
        // You can remove this part as it's no longer needed
        // playerDetected = Physics2D.OverlapBox(doorPos.position, new Vector2(width, height), 0, whatIsPlayer);

        // This part is no longer needed since you're using OnTrigger
        // if (playerDetected == true)
        // {
        //     if (Input.GetKeyDown(KeyCode.E))
        //     {
        //         sceneSwitch.SwitchScene(sceneName);
        //     }
        // }
    }

    // Use OnTriggerEnter2D instead of Update
    private void OnTriggerEnter2D(Collider2D other)
    {
        // Check if the collider is the player using layers or tags
        if (whatIsPlayer == (whatIsPlayer | (1 << other.gameObject.layer)))
        {
            sceneSwitch.SwitchScene(sceneName);
        }
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireCube(doorPos.position, new Vector3(width, height, 1));
    }
}

this transition was originally set with a key E, I changed it to react with a triggered collider2D.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneSwitch : MonoBehaviour
{
    public static string prevScene;
    public static string currentScene;

    public virtual void Start()
    {
        currentScene = SceneManager.GetActiveScene().name;
    }

    public void SwitchScene(string sceneName)
    {
        prevScene = currentScene;
        SceneManager.LoadScene(sceneName);
        Debug.Log("Switching scene from " + currentScene + " to " + sceneName);
    }
}

Here is only Debug.Log that chat added.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SceneSwitchTrigger : MonoBehaviour
{
    SceneSwitch sceneSwitch;

    [SerializeField]
    private string sceneName;
    private void Start()
    {
        sceneSwitch = FindObjectOfType<SceneSwitch>();
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            sceneSwitch.SwitchScene(sceneName);
        }
    }
}

This was a script that was part of a youtube video I was studying. But i ended not using it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class World : SceneSwitch
{
    public Transform player;
    public float posX;
    public float posY;

    public string previous;

    public override void Start()
    {
        base.Start();

        Debug.Log("Previous Scene: " + prevScene);
        Debug.Log("Expected Previous Scene: " + previous);

        if (prevScene == previous)
        {
            StartCoroutine(SetPlayerPositionAfterTransition());
        }
    }

    private IEnumerator SetPlayerPositionAfterTransition()
    {
        // Wait for the end of the frame to ensure the transition has occurred
        yield return new WaitForEndOfFrame();

        Debug.Log("Setting player position to: " + posX + ", " + posY);
        player.position = new Vector2(posX, posY);
    }
}

This was also original script from yt, but gpt added IEnumerator.
Also before I added these script there, I already had starting point in a from of a script:

using UnityEngine;

public class GameManager : MonoBehaviour
{
    public GameObject startingPosition;
    

    public static GameManager Instance; // Singleton pattern

    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
        {
            Destroy(gameObject);
        }

        DontDestroyOnLoad(gameObject); // Keep GameManager alive between scenes
    }

   
}

But I found out, that when this script is active in game, it disturbs the World script. What happened is that when I changed a scene, Player transform wa missing object suddenly.

I do understand that making a good scene transition is probably more complex thing. But I would like to know my options and opinions of someone else, more… living.

Leave a Comment