Starting point for leaving each scene?

hello! Im new to unity so please bear with me!
Im trying my hand at making a game in 2D where you can enter houses and exit back to the main menu.
I have 2 questions!
1- is there a way to set the Player to be facing a certain way when entering a house?
2- can I set different starting points for the main map depending on where you exit from?

thank you!

Short answer is yes, you can absolutely do all that.


To give you more direction, it really depends on a lot of what you already have written, but depending on your proficiency with code you might be able to take my advice and utilize it, but if you want a plug and play solution that you can just copy from here and paste into your game it depends on how you instantiate the player when loading scenes mainly and how many scripts are involved with that process.


To give you general direction, you can save a bool in PlayerPrefs like this:

PlayerPrefs.SetInt("FaceLeft"),   1 or 0);
PlayerPrefs.GetInt("FaceLeft"),  1 or 0);

And the reason I mention that is you will need to create a bool that will notify your instantiation script what direciton you plan on having them face when being instantiated (the logic behind that is that 1 will be true, 0 will be false, and when you set an int to 1 or 0 like that, you get a handy bool that isn’t supported by playerprefs without a little creativity) and I would put a public bool shouldFaceLeft on the door script to trigger the event:

OnTriggerEnter2D(Collider col)
	 {
		if(shouldFaceLeft == true)
		{
			PlayerPrefs.SetInt("FaceLeft", 1);			
		}
	 }  

Then when you load the scene, on whatever script that instantiates your player into the scene, do a check like this:

if(PlayerPrefs.GetInt("FaceLeft") == 1)
{
     spriteRendererReference.flipX = true;
}

And that will be able to flip the sprite as it’s loaded.


As far as instantiating at a certain point on the main map, this might get you in the right direction, but I would create:

public List<Transform> playerPositions = new List<Transform>();

of starting points on your main map, and then reference that list based on list[int] in the playerprefs again; so for example if you want to have list[0] = (0,0,0), then in your main scene, create an empty game object at that position, add publicly to the list as the first element, and in the door collider script where you set the bool, also set a public int spawnNumber (for this example, set spawnNumber to 0 in the inspector) and then do this:

PlayerPrefs.SetInt("Spawn", spawnNumber);

And again in your scene loading script do something like this:

Vector3 startingPosition = playerPositions[ PlayerPrefs.GetInt("Spawn")].position;

And then you can publicly populate the rest of your starting positions with empty game objects peppered throughout the main scene at the places you want player to start; when you want to reference a different position in the list, just make sure that door collider has the spawnNumber setup correctly. REMEMBER, LISTS AND ARRAYS BEGIN WITH 0, NOT 1.


NONE OF THIS IS TESTED, NONE OF THIS WAS WRITTEN IN AN IDE, THERE MAY BE SYNTAX AND COMPILER ERRORS, BUT THIS SHOULD BE A SOLUTION TO AT LEAST GET YOU STARTED