- Home /
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?
I am a little confused. Where is 'playerPausedPosition' and 'playerPauseDirection' being set?
Currently at the top of the script in the Start function:
void Start()
{
playerPausePosition = new Vector3(0f, 0.01f,-102f);
playerPauseDirection = new Vector3(0f, 0f, 0f);
}
I'm basically just trying to store/set the player's direction to 0 facing forward by default (presumably in the world axis) when I pause the game.
I've also tried using public variables and setting them in the inspector, and also quaternions and localRotation too, and that doesn't help either.
Note: I'm using Unity's built in PlayerController for the player (and the Oculus one when in VR mode), which I worry might be overriding my code or something.
How is the original code you posted called? OnClick()? Then does an if check if the game is paused? Or is that in an update?
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()
{
myRotation = transform.rotation;
}
Answer by darkStar27 · Dec 07, 2018 at 04:32 AM
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.
I can't do this unfortunately because I actually need the player to still be able to move and turn and shoot and stuff when in the pause screen, as the pause screen is just another room directly in the game where the player shoots the various options, which is particularly suited for when in VR mode.
Also, I did try the timescale stuff and it was pretty cool for just pausing most of the game in general but it caused some of its own issues along with the player stuff I mentioned.
What you can do is when the player presses the escape key or whatever button he is suppose to, to pause the game, you can record the transform of player and assign it when needed.
What i mean is rather then checking condition for whether the game is paused or not, you can check the condition when the player presses the button.
As far as I'm aware that's what I am doing.
Answer by nicholasw1816 · Dec 10, 2018 at 02:08 PM
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;
}