Hi.
thanks for any help !
i need when the player press a mouse button
save the player position x,y,z
then when press a second button
go to saved position
I already have this code but not working:
function save_position () {
PlayerPrefs.SetInt("PlayerPosX", newSpawnPoint.transform.position.x);
PlayerPrefs.SetInt("PlayerPosY", newSpawnPoint.transform.position.y);
PlayerPrefs.SetInt("PlayerPosZ", newSpawnPoint.transform.position.z);
}
function relocation () {
player_position_spawn = PlayerPrefs.GetInt("PlayerPosX");
my_player.transform.position = player_position_spawn.position;
player_position_spawn = PlayerPrefs.GetInt("PlayerPosY");
my_player.transform.position = player_position_spawn.position;
player_position_spawn = PlayerPrefs.GetInt("PlayerPosZ");
my_player.transform.position = player_position_spawn.position;
}
the error message when try to relocate player:
(picture below)
thanks for any help on this
//

Try puting the script into Standard Assets folder and see if it works.
Without knowing what type your variables are this could be wrong but:
This line
PlayerPrefs.SetInt(“PlayerPosX”, newSpawnPoint.transform.position.x);
Would be saving just a number like 20 (I would have thought a float would be better unless the player does only move by whole numbers)
Then you get this number with
player_position_spawn = PlayerPrefs.GetInt(“PlayerPosX”);
which would result in player_position_spawn = 20
Then you try and use this variable to set the players new position x
my_player.transform.position = player_position_spawn.position;
Why did you stick .position onto the end of the variable player_position_spawn.position it doesnt seem right.
I would have thought you need something like this:
function relocation () {
player_position_spawn_X = PlayerPrefs.GetInt("PlayerPosX");
my_player.transform.position.x = player_position_spawn_X;
player_position_spawn_Y = PlayerPrefs.GetInt("PlayerPosY");
my_player.transform.position.y = player_position_spawn_Y;
player_position_spawn_Z = PlayerPrefs.GetInt("PlayerPosZ");
my_player.transform.position.z = player_position_spawn_Z;
}
But as I said without the variable types I could be wrong
There are a few obvious problems…
First, a “position” is a Vector3. The x, y, and z components of a Vector3 are of type float, not int. So, you’re probably going to want to use PlayerPrefs.SetFloat and PlayerPrefs.GetFloat instead of the Get/SetInt methods you’re using now.
Next, when you retrieve the stored vaues, you’re retrieving a simple, scalar value - not a “position” object. Assuming you make the above-mentioned change, the retrieved value will be a simple float.
So, to use it, you need to assign it to the appropriate member of the position object. So, something like this:
float xVal = PlayerPrefs.GetFloat("PlayerPosX");
my_player.transform.position.x = xVal;
Note, I’m a C# user, but I think the above syntax is OK in US…
Jeff
thanks so much …
i solve it with the code :
note: but without float “word” at the begining
here is the code
thanks again … now my small question is
when resume a saved game
how to tell unity3d to relocate the last player position when saved please ?
now is working on the same scene …
but when I quit the saved game and reopen …
unity send the player to the level saved … but NOT to the last player position on that level.
please help
Not sure I understand the problem. Why not just…
- Load the saved level
- Restore the player’s position to the saved position using (something like) the above code?
So, just set the player’s position to the loaded x, y, and z values…
Jeff
ok …
thanks so much for all your invaluable help…
i try first and reload the saved level…
but when try to re-locate the last player position of the player is not working…
i think my problem is how tell to unity3d who will relocate…
i explain :
when you are on level 3 for example …
and save the level and location
then quit
then reload a resume game …
but the problem is when try to reload the player position
how can I tell unity3d who is newSpawnPoint ?
because you are on menu scene calling level 3, and … on menu scene you dont have a player
got it ?
any idea please ?