- Home /
Load a Scene and Move the Player to an Specific Location.
Hi to everybody! I have read many answers about this issue, there are a lot of answers about this, with many kind of solutions, however I have been reading them during a week and I still don't understand them, perhaps 'cause I'm starting to learn programing and I really would like someone help me with this:
I'm the player, and actually I'm in a big town with many places to go, but I found a coliseum and I want to enter to it. That big town is actually a scene called "Town" and obviously the internal area of this coliseum is another scene called "Coliseum".
However, this coliseum has 4 doors. All these doors lead me to the internal area, but the position is different because one door is in north, other is in south, other in the east and the other in the west. And when I get back to the town, the corresponding door must lead me to the location where I was in the town. I think this is like when we explore a huge mansion with many doors and rooms...
Many answers say something about to save player last location, but I really didn't understand it. I really prefer to use a script which lead me to the other level and at the same time to move the player to the location I want in that new level, like when we write it using Vector 3.
What script should I use? To which GameObject do I have to apply? To the trigger zone? To the player? Both? To another GameObject? I really would like you can explain me this, please.
Thanks for you help and your time!
Answer by Screenhog · Aug 02, 2012 at 04:37 PM
I would probably build a master GameObject that controls game functions, with a script on it that says DontDestroyOnLoad. (This means that the gameObject will continue to exist from scene to scene.) That script would have a variable called "doorID". Then, in the town, each door to go into a building would have its own unique ID number, similar to what strachpr01 said. Every time you hit a door trigger, set "doorID" to the ID of the door you collided with.
When you load the new scene, check which door was most recently collided with, and set the entry position of the new scene based on that.
Yes I did start off by thinking of a game manager object. But if I read it right the objects are in separate scenes so unless the positions of the doors are the same in each scene then passing the specific vector across will not work. That's why I changed my $$anonymous$$e and went with the above. If the object are in the same position I would definitely use this one.
It could still work. Upon entering the "Coliseum" level, you'd have a script that:
checks which door was used to get in
look for a door in that scene with the same door ID number
go to the position of that door
Answer by strachpr01 · Aug 02, 2012 at 04:06 PM
in the new scene create 4 empty game objects one at each internal door. when the player hits the door collider in the first scene set an int to be 1,2,3 or 4 depending on which door they have hit, you can then reference this in a spawn function for the new scene. you then dont need to worry about getting the vector3 from the first level. in the new scene you just create a 'public Transform[] spawnPoint' variable in a script. drop the four empty game objects into the inspector and in the Start() function set the players position to be the same as the correspoding spawnpoint. so
if(ExampleInt==1){
transform.position = new Vector3(spawnPoint[0].position);
}
just pop this script onto the player and it will move when the level loads
Answer by reptilebeats · Aug 02, 2012 at 05:39 PM
wild stab in the dark but could you not just put a script on the door that loads the level at a specified position in the next level. and then the script is destroyed on load.
var location : vector3
when door is hit. dont destroy on load
load level,
when level is loaded apply location and then destroy this script
Answer by Nicosim87 · Aug 03, 2012 at 04:55 PM
I already made a test with Michael Gray's solution, but now I have more questions. I couldn't use the var location, because looks like it wasn't read, but I used the DontDestroyOnLoad function, which it managed to take me to the Coliseum, keeping the GameObject with tag("Player") position, however when that happens, the render screen turns gray and I can't see a thing, except in the preview window, and I realized my character survived.
Additionaly, the initial player doesn't destroy when I get back to the Town. Gray is telling me about a function which destroys a script. How can I do that with the initial player?
Look this is what I have in my scripts.
SCRIPT APPLIED TO THE TRIGGER ZONE IN TOWN WHICH LEADS ME TO THE COLISEUM:
var target : Transform;
function OnTriggerEnter ()
{
DontDestroyOnLoad (target.gameObject);
Application.LoadLevel ("Coliseum - Internal Zone")
}
function Update(){
}
SCRIPT APPLIED TO THE TRIGGER ZONE IN COLISEUM WHICH LEADS ME TO THE TOWN:
function OnTriggerEnter(other : Collider) {
if (other.tag == "Player") {
DontDestroyOnLoad (other.gameObject);
Application.LoadLevel ("Town");
}
}
The strange part is, if I start to play since the Coliseum and I go to Town, the screen is normal, but if I go to Coliseum from Town, the screen turns gray. By the way, the main camera is attached to the player, I already confirmed when the character survives, the camera also does.
Ignore the gray screen. I already solved that problem, something was hapenning to the player.
i use
function OnLevelWasLoaded(){
}
this is a built in function which just says when the application loads a new scene. and then in there i have Destroy(GameObject); to destroy the object i just carried over.
Wow!!! Thanks a lot! I managed to destroy the gameObject using also a public var!
i will mention that if you reload the level after the script has been deleted your character may end up somewhere else, so you may want to have a variable set on your character or something that stores the position and then if you reload the current level then use that variable.
but make sure to have it on something like the character which will keep all its properties when a level is loaded. and if you save the game to save it after the position has been stored, or some games will have checkpoints or if its like fallout it can save anywhere, you can save the position when you save it.
Answer by Rispat-Momit · Sep 15, 2013 at 08:34 AM
Here is a script that I made, it loads a level, changing player's position
/*
1. Create a trigger and assign this script
2. Create a trigger where the player is going to be loaded
3. Complete the script
*/
var MyLevel:String;
var MyPlayer:GameObject;
var Destination : Transform;
function OnTriggerEnter (other : Collider) {
if (other == MyPlayer.collider) {
Application.LoadLevel (MyLevel);
}
}
function OnLevelWasLoaded(){
MyPlayer.transform.position = Destination.position;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613075054im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera rotation around player while following. 6 Answers
Change scenes from area? 1 Answer
How Move Player between Scenes? 7 Answers
How to keep position of a Game Object when switching scene? 3 Answers
Can I utilize LoadLevelAdditive for immersive game? 2 Answers