Spawn Player in Desired Location and Facing Correct Direction in Scene Change

In case it’s useful to anyone, I found a tutorial that shows how to place the Player game object in the correct location when they move between two scenes (ie: when they walk in and out of a building) using a scriptable object instead of a singleton.

This places the Player near the doorway that they just entered or exited.

I modified the code a bit so that the character also faces the correct direction in the next scene.

The main flow of logic is…

  1. The game object with collider set to trigger next scene will have fields to set the Player position (a Vector2) for the next scene and get the X and Y float values of the Animator Vector2 (to get the direction Player was facing when collider was hit).

  2. The Player position Vector2 and the Animator Vector2 values are stored in a scriptable object (data container) to be used in the next scene.

  3. The stored values are passed back into the Player (Movement) Controller in the Start() so that the Player game object spawns in the specified location and is facing the same direction as they were in the previous scene.

To demonstrate that it works, I set up this random structure with four entrances. Each entrance on the outside (in first scene) and each exit on the inside (of the second scene) has a trigger that will tell the character where to stand in the next scene and which way to face.

Edit: Unfortunately, I can only post gifs, so I can only show part of the example.

While the project is private, I’ve put the main scripts in a public repo:

And here is the tutorial I based the scripts on:

Foor_Door_Room_Short_2_AdobeExpress





Privacy & Terms