Search code examples
unity-game-enginescene

Switch between scenes but keep player position when comes back?


My game will switch between two scenes : scene A and scene B;
scene A is a world where the hero can walk around and trigger battles;
scene B is the battle scene;

when the battle finished, I want to turn back to scene A and hero should be in the position where it trigger battles. So I need to save scene A before I load scene B;

I tried the api LoadSceneMode.Additive; But it's just used to mix one scene to the current loaded scenes.

Could you help me plz?


Solution

  • Firstly DO NOT use "additive". Just use ordinary scene load.

    Secondly you have the problem of "remembering" where the guy was when sceneA loads.

    Your easiest approach to get you going .. learn about PlayerPrefs.

    Just before you quite sceneA, save the hero's position. When you load sceneA, get the hero's position.

    Alternately you can use one static class as a sort of global to keep track of the info. But to do that you have to learn about writing that sort of code.

    Be aware that what you're doing is not that easy - Unity is a lot harder than it says on the box.

    I encourage you to master PlayerPrefs in the first instance, because you will have to use it all the time anyway.