- Home /
Connect multiple scenes to one main scene
So I'm making a 2d RPG (not to sell because I'm still pretty new with coding and just trying to learn) and my plan is to have multiple scenes (which will act as NPC houses) and allow the player to explore each house.
But my problem comes from trying to return to the main scene, the world_scene. I want the player too, when exiting one of the house scenes, to appear outside that house in the main world_scene. But he just pops back to whatever position he was in when I created the main world_scene. I know this is because I'm unloading the world_scene, than reloading the same way it was when I first created it.
Throwing a DontDestroyOnLoad on my player leads to the player spawning at (0, 0, 0) in whatever scene he switches to.
So my question is, how can create a way for my player to travel to any NPC house (which will load a new scene) and have him then reload the main_world scene at the same position he was at when he first loaded the new scene.
Been stuck on this for a while, any help will be much appreciated!
persistently save the position before loading a house. use it, if available, ins$$anonymous$$d of the default spawn location. PlayerPrefs can be of help, a static spawn position variable...
Answer by pruthv · Dec 13, 2018 at 06:46 AM
Try storing player's position using PlayerPrefs just before you exit the World scene. Now, when your player come back to World scene from other scene, read and set player's position using PlayerPrefs.
Just make sure you also have an If statement running if there is no value stored in PlayerPrefs, which will put player into the initial position of World scene.
I don't know how you're triggering the scene change, if you can provide some code than I can help you with hard coding this.
public class PlayerLoadNewScene : $$anonymous$$onoBehaviour
{
[SerializeField] private string sceneToLoad;
private bool canLoadScene;
private void Update()
{
LoadNewScene();
}
private void LoadNewScene()
{
if (canLoadScene == true && Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
{
Scene$$anonymous$$anager.LoadSceneAsync(sceneToLoad);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
canLoadScene = true;
}
}
I stripped down to the basics after a few failed attempts at implementing this feature. But the idea is that, when a player enters a box collider 2D, they will be able to press 'space' and then load that scene. I also Serialized a string which will allow me to type what scene I want to load in the inspector. I thought this could help with prefabing my load script on an empty gameobject to allow for easy setup when I want to add a new scene that the player can load.
Thanks for the quick reply!
@Shnuckles There you go... Unity doesn't allow to store Vector2 in PlayerPrefs so I'm using individual x and y float values to store the position as you said it is 2D game. Let me know if you encounter any issues
public class PlayerLoadNewScene : $$anonymous$$onoBehaviour
{
[SerializeField] private string sceneToLoad;
private bool canLoadScene;
public GameObject player; // your Player or NPC whose position you want to set
private float xPosition; // Storing last known x and y position in this
private float yPosition;
private Vector2 playerPos; // used for combining xPosition and yPosition into Vector2, just safe practice
private float startXpos; // Initial position when you load the World scene for the first time
private float startYpos;
private void Start()
{
xPosition = PlayerPrefs.GetFloat("X Position", startXpos); // Gets xPosition if it stored, else will take startXPos for default
yPosition = PlayerPrefs.GetFloat("Y Position", startYpos);
playerPos = new Vector2(xPosition, yPosition); // combining x and y into Vector2
player.transform.position = playerPos; // assigning player position
}
private void Update()
{
LoadNewScene();
}
private void LoadNewScene()
{
if (canLoadScene == true && Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
{
Scene$$anonymous$$anager.LoadSceneAsync(sceneToLoad);
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
xPosition = player.transform.position.x; // using same variable to store the new position before exiting the scene,
yPosition = player.transform.position.y; // however if you get issues than I would suggest you to create new variable for this.
PlayerPrefs.SetFloat("X Position", xPosition); // storing the player position values
PlayerPrefs.SetFloat("Y Position", yPosition);
canLoadScene = true;
}
}
@pruthv Thanks! The only thing I changed was that I put the extra functionality that you put in the OnTriggerEnter2D into the LoadNewScene if statement. So now when I press space it will save my position.
Thanks for your help!