- Home /
change player location when coming back to a level
I am making a project where I need to exit one part of a level such as a door way and when I come back I start from that door way. I've read up on Scene connecting and tried to make that work as best as possible but have problems. One my scene manager game object that holds info I put a Dontdestroyonload on it to keep information but I end up with repeats of of that game object I was pointed towards the direction of Singletons but have no clue what to do with them. I am using a OnLevelwasLoaded to keep track of what end of the level I'm at how do I program that to tell the player what part of the level to start from again
function Awake () { DontDestroyOnLoad (transform.gameObject); } function OnLevelWasLoaded (level : int) { if (level == 12) { print ("backfromdiary");
}
if (level == 11)
{
print ("backfromphone");
}
if (level == 13)
{
print ("backfromtable");
}
}
Answer by kalvinlyle · Mar 11, 2012 at 05:14 AM
If you have a do not destroy on load object you need to make sure you don't load more of them with your new levels. Create an empty scene with just your do not destroy objects, in the Awake() function put a call to load your level. Make sure your levels do not have any do not destroy on load objects in them.
On the player Start() function do the check to see what level was the previous loaded level (you'll have to store that in your donotdestroy object and once you set the player position then set that variable to the current level)
Answer by Kleptomaniac · Mar 11, 2012 at 10:09 AM
What you can do is before you load each level with Application.LoadLevel
, log the player's last position to an array of Vector3's, with each element corresponding to the appropriate level. Then, when calling OnLevelWasLoaded
, look for the appropriate array element and change the player's position to that position. So for example:
var player : GameObject;
private var lastPos : Vector3[]; //In order for this to work, you should call each level as consecutive numbers from 0 - e.g 0, 1, 2, 3
private var levelToLoad : int;
private var canLoadLevel : boolean = false;
function Awake () {
DontDestroyOnLoad(transform.gameObject);
lastPos = new Vector3[Application.levelCount - 1];
}
function Update () {
if (canLoadLevel) {
lastPos[Application.loadedLevel] = player.transform.position;
Application.loadLevel(levelToLoad);
canLoadLevel = false;
}
}
function OnLevelWasLoaded(level : int) {
player.transform.position = lastPos[level];
}
Created on air, so I don't know if it will work per se. In order for it to work, you'll need to make it so that your levels are named in a consecutive order from 0 (e.g. 0, 1, 2, 3, etc.) and you whenever you need to load a new level in another script, change canLoadLevel
to true, levelToLoad
to whatever number level you want to load, and then change canLoadLevel
back to false. There is probably a better way of doing this, but right now that's all I could think of to keep it nice and short.
Hope that helps! Klep
It seems way too complicated. I'm hoping that there is a way to just tell a scene that when loading from level a to level b that it sets the player controller at a specific point. when loading from level c back to level b it sets the player controller to a different specific point.