I’m making a game in Unity, and I’m trying to use PlayerPrefs to save the player’s location. I want a script that will save the player’s position every few seconds and then when I close the game and reopen it, it will load the last place I was. Here is the code I have so far. Please help!
//saving it
var PlayerPosition = Vector3(0,0,0);
var PlayerCurrentPosition: ; //constantly changes - time function
var SavedPlayerPosition: ;
function Update ()
{
PlayerPrefs.SetFloat("PlayerX", transform.position.x);
PlayerPrefs.SetFloat("PlayerY", transform.position.y);
PlayerPrefs.SetFloat("PlayerZ", transform.position.z);
}
//loading it back - when to load it - on load, game start
function Update (loadPosition)
{
if () {
Vector3 newPosition = Vector3 ("PlayerX", "PlayerY", "PlayerZ");
newPosition.x = PlayerPrefs.GetFloat("PlayerX");
newPosition.x = PlayerPrefs.GetFloat("PlayerY");
newPosition.x = PlayerPrefs.GetFloat("PlayerZ");
transform.position = newPosition;
}