Setting player position based on which scene current scene was loaded from?

Hi everyone,

How do I set different spawn points based on which scene the correct scene was loaded from?

Basically it is a maze game and at certain points the player walks into a trigger which loads a new scene (a puzzle of sorts) - they solve the puzzle and return to the maze, but I want them to spawn to approximately where they left off from in the maze.

It doesn’t have to be as complex as a save/load system. I need to figure out how to get the script to identify which scene the current scene was loaded from either by string or build index and then maybe an if statement to say if this scene was loaded from “Puzzle1” then player.transform.position = new Vector 3 ()…

Can anyone offer some pointers?

Many thanks!

J

Are there multiple crossover points between two scenes in the maze?

You could store the scene name somewhere like PlayerPrefs, and refer back to it.

However, I feel a more elegant solution would be to place a gameobject and attach an empty “SpawnPoint” script, then when the new scene loads, do a search for the SpawnPoint gameobject (using FindObjectOfType) and place the player there. No need to keep track of names.

You could have an object set to don’t destroy on load. On that object you have a script where you save your current transform.position before scene change. When you return to the maze you check for that object and if there is a previous position stored you spawn the player at that position.

Thanks for the input guys.

Pete - the scene changes are as follows (or should be):

Maze: player move to Trigger 1 which loads Puzzle1
Complete Puzzle1, back to Maze at transform of Trigger 1 (now deactivated)
Maze: player move to Trigger 2 which loads Puzzle2
Complete Puzzle2, back to Maze at transform of Trigger 2 (now deactivated)
Maze: player move to Trigger 3 which loads Puzzle3
Complete Puzzle2, back to Maze at transform of Trigger 3 (now deactivated). Go to end of Maze, game complete! There will be no deviation from that level structure.

Joe, can you offer a little more information? I had figured out the DontDestroyOnLoad part - but struggling to get the logic of the script/s.

  • So on this script I can declare a public GameObject called mazePlayer - drag the player into public field in inspector.
  • Declare a Vector3 called playerPosition.
  • On update do playerPosition = mazePlayer.transform.position;

What comes next? How do I set player position to playerPosition when Maze level loads again?

Thanks for your patience!