Home > Net >  saving player position while moving between scenes Unity 3D
saving player position while moving between scenes Unity 3D

Time:08-16

I've started learning 3D game development and now I've reached the scenes changing. I looked over the internet and I couldn't find a solution that I can implement in my project.

At this point, I've two scenes, the street which is the main scene, and a bar scene. I've created a sliding door that has a collider called portal that triggers the scene switch.

I've managed to swap the scene but when I'm trying to go back to my main scene I respawn and the starting point and not at the bar exit. I've tried to save the player position in a temp variable but that didn't work well. what should I do to make my player start at the store exit ?

public class PlayerMotion : MonoBehaviour
{
    //...
    public static Vector3 playerPosition; // for respawning use
    public static bool respawnNeeded = false;
    CharacterController cController;

void Update()
    {
        if(respawnNeeded)
        {
            cController.transform.position = new Vector3(StreetToBarFFPortal.tempPosition.x,
                                                        StreetToBarFFPortal.tempPosition.y,
                                                        StreetToBarFFPortal.tempPosition.z);
            respawnNeeded = false;
        }
         //..... some movement code
        playerPosition = new Vector3(cController.transform.position.x, cController.transform.position.y, cController.transform.position.z);
}

public class StreetToBarFFPortal : MonoBehaviour
{
    public static Vector3 tempPosition;
    private void OnTriggerEnter(Collider other)
    {
        if(other.CompareTag("Player"))
        {
        int index = SceneManager.GetActiveScene().buildIndex;
            if(index==0)
            {
                PlayerMotion.respawnNeeded = true;
                tempPosition = new Vector3(PlayerMotion.playerPosition.x,
                    PlayerMotion.playerPosition.y,
                    PlayerMotion.playerPosition.z-4);
            }
         index = 1 - index;// 1 transfers to 0 and 0 transfers to 1
        SceneManager.LoadScene(index);
        }
    }
}

I also have a GlobalManeger script with a singeltone design pattern if that helps.

CodePudding user response:

You have tempPosition which as I can see used to store player position before teleportation, am I right?

So I can't see where this tempPosition variable came from. Is it static or from a singletone script? Anyway you need to have this variable through scenes. So it should be in the script that has DontDestroyOnLoad() method;

CodePudding user response:

You have two options:

  1. Store it in the DontDestroyOnLoad()

  2. Store it in playerprefs before you load the other scene and load it back when the scene is loaded again. Since you're storing a Vector3 then store the x, y and z like this:

    PlayerPrefs.SetFloat("X", playerPosition.x); PlayerPrefs.SetFloat("Y", playerPosition.y); PlayerPrefs.SetFloat("Z", playerPosition.z);

and load is like this:

float xpos = PlayerPrefs.GetFloat("X");
float ypos = PlayerPrefs.GetFloat("Y");
float zpos = PlayerPrefs.GetFloat("Z");
playerPosition = new Vector3(xpos, ypos, zpos);
  • Related