- Home /
[Please Help!]How do I make a player save their position when I pause the game?
How can I make the player save their current position in the scene when I press the P button to open pause menu and reload when the player continues their game after closing and reopening the game? What I'm trying to say is that if I open the pause menu I want the game to save their position and then load after closing the menu.
Please provide a script, tutorial, and try to make it c#. Thanks so much if you can.
Answer by AngryCSharpNerd · May 22, 2013 at 09:08 PM
I'm sorry that I'm not a a C# person, but basically, it would be like:
Vector3 savedPos;
Transform player;
bool isPaused = false;
void Update(){
if(Input.GetKey("P") && isPaused == false){
OnPause();
}
else if(Input.GetKey("P") && isPaused == true){
UnPause();
}
}
void OnPause(){
savedPos = player.position;
isPaused = true;
player.position = savedPos;
}
void UnPause(){
player.position = savedPos;
isPaused = false;
}
Dude, this is wonderful. I could do JavaScript too. Thanks like a whole lot. This is truly great.
Whutchison
@Whutchison please set the answer as true, as I see your comment, you can also up vote as well, but a correct answer has to be set as true:)
Ok, if I pause it, will the screen like freeze, or will text appear or something to let me or the player know that the game is paused? I see that it will pause and it will save the position but how do I know if the game has been stopped or paused?
Actually, by pausing, nothing will happen and neither will text freeze. That's just saving the position for what I wrote. So you'll need to do an OnGUI() function, I don't know C# too much (irony of my name) so you can look that up elsewhere and have an if statement of "if(isPaused == true)".
//Also, add this into the **O**nPause() function:
AudioListener.pause = true; //This will pause sound from co$$anonymous$$g in
AudioListener.volume = 0; //Just to make sure, no volume either
Time.timeScale = 0.0; //This is what pauses the game. Say 0.5 and it's slo-mo!
//Add this into the **U**nPause() function:
AudioListener.pause = false; //Have sound play
AudioListener.volume = 1; //Sound up
Time.timeScale = 1.0; //Back to normal
(You might need to add an 'F' to the numbers because it's C#, that means "float")
Ask me anything if you need more help
Answer by roojerry · May 22, 2013 at 08:42 PM
you could look at PlayerPrefs for a place to store that position data.
Answer by Stormizin · May 22, 2013 at 08:38 PM
Use transform.position and insert in the database. Then when you start the game again instantiate him in this last transform.position The transform.position will return you the (x,y,z) in the world.
Isn't instantiate used in projectiles like bullets? Or is a much more of a broad function?