- Home /
Choosing where a player spawns on the loading of a scene
First, I would like to say that I am new to unity so I'm not sure if this is an easy problem or not, I just can't find a solution. So, I have 1 scene called the village scene that contains a bunch of houses (that each have their own scenes) that I can enter via their doors using the below script that I got from someone else.
public string sceneToLoad;
public void OnTriggerEnter2D(Collider2D other){
if(other.CompareTag("Player") && !other.isTrigger){
SceneManager.LoadScene(sceneToLoad);
}
}
The problem isn't that, as that just takes you to where the player is positioned in the building that is loaded. From each of the scenes that are the interiors of the houses, if I apply that script to an exit, it can only put the player in one position - where it's positioned in the scene. How do I, on the player exiting the building scenes, position the player in a specific position in the village scene depending on what scene was just exited?
Answer by Caeser_21 · Apr 11 at 10:00 AM
Incase you want to teleport the player to the exact point from where he entered the house, you could try :
public string sceneToLoad;
public void OnTriggerEnter2D(Collider2D other){
if(other.CompareTag("Player") && !other.isTrigger){
float x = other.gameObject.transform.position.x;
float y = other.gameObject.transform.position.y;
float z = other.gameObject.transform.position.z;
PlayerPrefs.SetFloat("X", x);
PlayerPrefs.SetFloat("Y", y);
PlayerPrefs.SetFloat("Z", z);
SceneManager.LoadScene(sceneToLoad);
}
}
Then in the script where you load the 'Village' scene, add this :
void Start()
{
if (PlayerPrefs.HasKey("X") && PlayerPrefs.HasKey("Z") && PlayerPrefs.HasKey("Z"))
{
float newX = PlayerPrefs.GetFloat("X");
float newY = PlayerPrefs.GetFloat("X");
float newZ = PlayerPrefs.GetFloat("X");
Player.transform.position = new Vector3(newX, newY, newZ);
}
}
Answer by DevManuel · Apr 11 at 09:24 AM
One suggestion could be to save the name of the current "house scene" (for example using prefabs). Then when you load your "village scene" simply read this variable and place your player where you want by checking some conditions:
private void SaveData() {
PlayerPrefs.SetString("houseScene", SceneManager.GetActiveScene().name);
Debug.Log("data saved");
}
private string LoadData() // load stored data
{
string houseScene = "";
if (PlayerPrefs.HasKey("houseScene")) {
houseScene = PlayerPrefs.GetString("houseScene");
Debug.Log("data loaded!");
}else{
Debug.Log("no saved data!");
}
return houseScene;
}
void Start(){
string houseScene = loadData();
switch (houseScene){ // name of last house scene
case "house1":
// move the player to target position
player.positon = new Vector3(10,0,0);
break;
case "house2":
player.positon = new Vector3(10,10,0);
break;
default:
player.positon = new Vector3(0,0,0);
break;
}
}
The idea is to save your data when entering your "house scene" and loading it when entering your "village scene". Hope it helps you, happy coding, Manuel
Your answer
![](https://koobas.hobune.stream/wayback/20220613055232im_/https://answers.unity.com/themes/thub/images/avi.jpg)