When saving the game (to a JSON string) I have it save the coordinates of the player and the map they are on.
The First Person Controller has the tag of “Player”
var Location = new JSONObject
{
{"Map", Application.loadedLevel},
{"X" , playerPos.x},
{"Y" , playerPos.y},
{"Z" , playerPos.z}
};
Load in the saved Data…
Map=(int)PlayerObject.GetObject("Location").GetNumber("Map");
Debug.Log("MAP "+Map);
CoordX=(float)PlayerObject.GetObject("Location").GetNumber("X");
Debug.Log("X" + CoordX);
CoordY=(float)PlayerObject.GetObject("Location").GetNumber("Y");
Debug.Log("Y" +CoordY);
CoordZ=(float)PlayerObject.GetObject("Location").GetNumber("Z");
Debug.Log("Z" + CoordZ);
Everything looks good at this point.
Then I wrote a function that will translate the player to the saved coords
public void PositionPlayer(float CoordX, float CoordY, float CoordZ)
{
Debug.Log("Send the Player to previous coordinates!");
Vector3 ReturnCoord;
ReturnCoord.x=CoordX;
ReturnCoord.y=CoordY;
ReturnCoord.z=CoordZ;
playerObject.transform.Translate(ReturnCoord);
}
In this case, I saved in front a large landmark, so I can make sure it worked properly.
It seemed like it worked, only I was translated somewhere else, not where I expected to be. Where did I mess up?
Also What is the best way to get the right level loaded and still get the right coordinates?
Thanks!