- Home /
Teleporting/application.loadlevel
Hello. I'm making a hobby project in unity and I'v encountered a problem when going from one level to another. Heres a picture that sheds some light on what i mean:
Currently i only have 2 scenes; 1) main menu 2) level 1 & 2.
When i start the game through main menu, i get moved to a certain position in lvl 1 after that i can move between 1 and 2 by using trigger on collision+transform.position. Now I'm trying to expand by making more maps, but putting them into the same scene will generate too much lagg, so i moved the existing lvl 2 into its own scene and put it to use:
var destination :int;
function OnTriggerEnter(other : Collider) { if (other.tag == "Player") { Application.LoadLevel(destination); } }
Now comes the actual problem: When i go back into lvl 1 scene i teleport to the original starting location, when i should have teleported outside the entrance of lvl 2 (a cave). So basically i start the level 1 over.
This will become even bigger problem when i start making more maps as seen in the picture. Is there a way to teleport just like using transform.position method except between scenes or a way to use application.loadlevel(4) and add some extra parameters like if last map lvl 3 then instantiate spawnpoint at entrance of lvl 3 of 4th lvl(ugh hard to explain). Or any other ways that would be better that what im thinking of.
Thanx for help, sorry for the rant. ^_^
You'll need to save the state of the level and load it when you go back to it.
Just teleporting to the entrance of the other level will not solve the 'restart' problem, i.e. all scripts and enemies and whatever will reset.
Hmm... but if you look at the picture above; if i start from lvl 1, go to lvl 3 and then 4 then back to 1 it still wont work. If i understood what you ment.
What I meant is that you have to save the state of the level when you exit it, like 'freezing' the level, and then 'resume' it when you get back to it, regardless of where you got there.
Answer by Michael CMS · Nov 05, 2012 at 02:18 PM
Imho use a KISS approach (Keep it simple sir ) : have a static variable for the last position on each level, then on the level loading set the position of the player at the last position of the level you are entering.
It's only 3 floats per level, not a huge overhead, and you will avoid allot of non-transparent behavior.
Hmm sounds like what im looking for. How would i use this practically? Can you give an example or reference page?
Practically on your OnTrigger enter function you will save the current player position in a static Vector3 variable.
Then upon the load of a level, on a Awake function of a script that is attached to the player, you will set the player position to the static Vector3 variable according to that level. Here's some C# code sample (I'm C# coder)
class SavedPlayerPositions { static Vector3 Level1LastPlayerPosition = Level1StartPosition; static Vector3 Level2LastPlayerPosition = Level2StartPosition; ... //how many levels you got, you put the default start location as initial value }
// enter from level 1 to level 2 OnTriggerEnter(Collider other) { if (other.tag=="Player") { if (currentLevel=="Level1") { // probably allways true since this collider is in level 1 SavedPlayerPositions.Level1LastPlayerPosition = other.transform.position; } Application.LoadLevel(destination); } } // now you have the last position of the player saved // all you have to do is to modify the awake function of your player so the player is set to the location you want upon loading a level
class IPlaceThePlayerOnPosition : $$anonymous$$onobehavior { void Awake() { if (currentLevel=="Level1") { gameObject.transform.position = SavedPlayerPositions.Level1StartPosition; } else if (currentLevel="Level2") { gameObject.transform.position = SavedPlayerPositions.Level2StartPosition; } } }
Now if you run this it will initially place the player on the default positions of each level.
When you change the level the default position for that level will be set to the last position the player had.
When you return to the level, the player will be placed in the last position he was in the level.
Just make sure to disable the trigger for a while(since the last position will be on top of the trigger, and you might end up loading between level 1 and 2 all the time )
You can format code properly in a comment by writing it as an answer, formatting, then copying the text to the comment box. ^^
Thanks for that mate :) . I'm new to unity answers. :).
Your answer
Follow this Question
Related Questions
FadeAndLoadLevel? 1 Answer
Application.LoadLevel wont work 0 Answers
MissingMethodException error? 0 Answers
Moving gameobject relative to resolution 1 Answer
Transform position? 2 Answers