Why are my player's positions not being set correctly?

Here’s the code I’m using to store the player’s current position when I press the pause button, and I then jump him to a new position in the level (the place where I have created the 3D pause room), and return him back to the stored position when I resume the game again:

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

        player.transform.position = playerPausePosition; // Moves player to the pause position (as set in inspector)

        gamePaused = true;
    }
    else if (gamePaused == true)
    {
        player.transform.position = playerStoredPosition; // Returns player to stored position
        player.transform.forward = playerStoredForwardDirection; // Returns player to stored direction

        gamePaused = false;
    }

Or at least that’s how it’s supposed to work.

But the player doesn’t jump to the correct place I set for the playerPausePosition in the Inspector. Where he ends up is often quite a bit off from the position I’ve set, and the amount it’s off varies each time too. And when he returns to the playerStoredPosition he’s facing in the wrong direction if I turned at all when in the pause room.

So, it’s not jumping to the correct position in the pause room as set in the Inspector (it’s not even getting near the pause room location most of the time), and it’s not facing the player in the correct direction when I resume the game (which should be the direction he was facing when I pressed pause).

Any ideas what’s up with that?

In my experience characters can be slightly tricky to move with code. I’ve been working on a teleport script, and had lots of problem with setting the rotation. It turned out the character controller script kept track of rotation internally, so I had to edit it to expose a function to re-read the rotation from the transform.

Btw, if you keep the comments on second lines so they don’t wrap, the code will be much easier to read. Also you don’t need to compare a boolean with true or false, you can just use the value directly in an if statement. true == true evaluates to true, making the comparison redundant.