How can I change player position and save direction?

I’m going to jump my player to a new position in the level when I pause the game, which I will use as the pause screen/room, and I want to store their current position and direction as I press pause so when I resume the game they will be in the same place and facing the same way they were when I pressed pause, even if I move and turn around while in the pause screen/area.

So how do I store and set the player’s direction properly?

I’ve tried using the following when the pause button is pressed down:

if (!gamePaused)
            {
                playerStoredPosition = player.transform.position; // Stores the position of player in the level
                playerStoredDirection = player.transform.forward; // Stores the forward direction of player in the level

                player.transform.position = playerPausePosition; // Jumps player to pause position (set in Start)
                player.transform.forward = playerPauseDirection; // Turns player to pause direction (set in Start)

                gamePaused = true;
            }
            else
            {
                player.transform.position = playerStoredPosition; // Returns player to stored position
                player.transform.forward = playerStoredDirection; // Returns player to stored direction

                gamePaused = false;
            }

But it’s not working.

The storing of the position works fine, but when I resume the game the direction the player is facing is wherever I’ve turned when in the pause room rather than where I was facing when I actually pressed pause.

Any ideas how to get this to work properly?

You can try passing the game with

Time.timeScale = 0;

This will solve your problem as you do not need to store the player position and directions and whenever you need to unpause just set it to 1 again the game will resume from the last paused frame.

For more details on what is Time.timeScale and how to use it see here.

You say you can store position but not rotation? Create a quaternion variable in your script, then store the players rotation in the quaternion. Example

private Quaternion myRotation;
 
void SomeFunction()
{
    //Store your rotation
    myRotation = transform.rotation;
    //Now when you need to reapply rotation do this.
    transform.rotation = myRotation;
}