Switching scences without loosing players position
Hey guys!
I'm working on my first game in Unity and I have a big problem.
There are 2 worlds in this game and in each world there are 5 houses you can walk in. So my big problem is, how can i make a script, in which the postition of my player is saved? Every time when I leave the houses I start at the beginning and I have already tried it with DontDestroyOnThis and saving the players position, as explained here in the forum, but nothing worked.
I don't know if i have to make a new script with dontdestroyonthis and connect it to the Player or do i have to put the code inside of the "FristPersonController" script. By the way I'm using the Unity Standard Asset First Person Controller.
Please can someone tell me the easiest way to manage this. The level switches are made with function OnTriggerEnter () and Application.LoadLevel (). Maybe I can write something in the triggers to realize that? Please help me I'm in a hurry and thanks a lot.
Answer by Zodiarc · May 31, 2016 at 04:56 PM
There´s a function called DontDestroyOnLoad or similar. This should do the trick.
see http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Answer by Mmmpies · May 31, 2016 at 05:19 PM
Guess they didn't read that you'd tried that! :D
OK DontDestroyOnLoad is what you need BUT it's not the complete solution, for that you have to record the scene just before you load the next one. So put a variable in DDOL that will still know what the last scene was.
Then when the next scene loads you check what scene you came from. I just had a list of empty gameObjects and when I changed scenes I move the player instantly to the right position/rotation depending on the previous scene.
EDIT should have said really - you need Application.loadedLevelName to record the scene name just before you load the next one. Store that in Don't destroy and then pick the correct location for your player using if or case depending on how many you have.
O$$anonymous$$ thanks a lot, that really sounds good, but as i said I'm not good in program$$anonymous$$g. So maybe if you could give me an exact example that would really help out, if it's not to much trouble for you?
Well it looks like that's getting deprecated anyway so in your DDOL script hold a public int
public int lastLevel;
Then before you call for another Scene set that variable from the script that loads the level. I'll call the script that is DDOL GameSettings...
public GameSettings theGSscript;
public void DoorClickedOn()
{
theGSscript.lastLevel = Scene$$anonymous$$anager.GetActiveScene ().buildIndex;
// Now load the next Scene
Now I'm being clever here and the empty GameObject I create is tagged Spawn with a number after it, So Spawn0, Spawn1 etc.
When the scene load you just need to get that int from the DDOL file (along with all your saved player data). Then do this...
int i = theGSscript.lastLevel;
string SpawnPoint = "Spawn" + i.ToString ();
GameObject playerspawn = GameObject.FindGameObjectWithTag(SpawnPoint);
player.transform.position = playerspawn.transform.position;
So you just tag the empty GameObject Spawn2 if you know you'll be entering from the Scene with index 2 and it'll move your player to the right location.
You might also want to do the rotation. And that last bit of code should be done AFTER you instantiate the player. It means setting up an empty GO even if there's only one entrance but it's really not that much of a hardship.