- Home /
Enter / Exit multiple locations from a single "base" level
I have a main scene setup from where I want to be able to enter/exit different buildings and return to my main scene. Think about entering a house in Skyrim for example.
So far entering a building, and thus loading a new scene works fine. The main problem I am having at the moment is the scene return.
When returning from "Building_A" my player spawns in the inital location of my base level instead of ending up in front of the door I previously entered.
I have my "Player" setup with a DontDestroyOnLoad()
to carry over stuff I want to store over my multiple scenes. The Debug.Log also shows the current scene that is played.
using UnityEngine;
using System.Collections;
public class PlayerLoadLevel : MonoBehaviour {
public GameObject Controls;
public GameObject Spawn;
public string firstLoadedMainLevel;
void Awake (){
DontDestroyOnLoad(gameObject);
Controls = GameObject.Find ("sceneControls");
Controls.renderer.enabled = false;
}
void OnLevelWasLoaded (){
if(Application.loadedLevelName == "loading"){
Debug.Log ("Level loading loaded!");
}
if(Application.loadedLevelName == "MainScene"){
Debug.Log ("Level MainScene loaded!");
}
if (Application.loadedLevelName == "Level01_BuildingA") {
Debug.Log ("Level Level01_BuildingA loaded!");
}
}
}
Additionally, I placed two Empty Game Objects as Spawn Points in the base scene. One for the inital spawn when loading the base scene, and another one in front of the door to spawn when I exit this particular building. The target scene also has an Empty Game Object as a spawn point, which works fine as well (probably because it is the only one...)
I guess what I need to do now is to determine (what i intended with Application.loadedLevelName
) and store the scene the player is currently in, then call the corresponding spawn when entering my base scene.
Sounds easy in theory but unfortunately, I have no idea how to do that. It would be great if somebody could point me in the right direction.
Answer by GameVortex · Jan 27, 2015 at 02:40 PM
This can often be quite problematic. Obviously you do not want to have duplicates scenes for each possible SpawnPoint. There are many ways of going about this, but this is the one I have found to work best for me and my projects, it is quite complex but I will give an example of a simplified version:
What you want is to have a custom SpawnPoint script which you attach to your SpawnPoints. In this script you have an int ID variable so you can assign each SpawnPoint in a scene their own unique ID. Imagine you are doing this in the "Overworld" scene. So you put a SpawnPoint by the door you want to spawn from and assign it an ID.
//Example SpawnPoint
public class SpawnPoint : MonoBehaviour
{
public int spawnPointID;
}
Then you create a custom Connector script which with two variables. One int for defining levelID and one int for defining SpawnPointID. Then you attach this script to your exit collider in the house and input the Overworld level ID (build index) and the ID for the SpawnPoint on the Overworld.
//Example Connector
public class Connector : MonoBehaviour
{
public int levelID;
public int spawnPointID;
}
Then when the character goes into the exit collider you need to store the spawnPointID defined by the exit somewhere which will not be destroyed when loading a level. You can store it in your player character for example as "NextLevelSpawnPointID". Then load the level defined by the LevelID variable in the exit. When the loading is done you need to do a search for all SpawnPoints in the loaded level. For example by using GetObjectsOfType(typeof(SpawnPoint)). Then iterate through all SpawnPoints and find the one with the ID matching the stored NextLevelSpawnPointID and set your character position to match the SpawnPoint position.