- Home /
Change player position upon entering new scene
I have made a door (with the tag "Entrance1stRoom") in my game that when the player goes through, it activates a trigger, and then I change scenes to an underground part, but when the player returns (in another door in the new scene, this one with the tag "ExitUnderground1stRoom") to re-load the first scene the player appears in the middle of the level, as that is its starting position, i'd like to change the position of the player upon re-entering the first level. This is what i've tried:
public void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "Entrance1stRoom") { SceneManager.LoadScene("underground1stRoom"); } if (collision.tag == "ExitUnderground1stRoom") { SceneManager.LoadScene("1stRoom"); if (sceneName == "1stRoom") { rb.position = new Vector3 (-7.5f, 9.5f); } } }
P.S. Sorry for bad english
does the same player object exists in all scenes or there is another player object in every scene?
Answer by Caeser_21 · May 02 at 09:02 AM
You ahev to use PlayerPrefs for this, this is just an example of what you have to do :
PlayerPrefs.SetFloat("XPos", -7.5f);
PlayerPrefs.SetFloat("YPos", 9.5f);
Then when you want to change the position, do :
float X = PlayerPrefs.GetFloat("XPos");
float Y = PlayerPrefs.GetFloat("YPos");
Player.transform.position = new Vector2(X, Y);
Your answer