- Home /
How can I save and load a player's position?
I'm designing a virtual navigation experiment, and i was wondering if i could somehow automatize my work. I need to present the same scene again and again, every presentation consists of adjusting player position first, than navigation, than if the subject says ready, save his position. So i thought about that maybe an algorithm could do it. The pure algorithm is something similar:
Load player position Let the player move the avatar Waiting for the subject to press ready Save position Do it again (approximately 20 times)
Could you help me out?
Agoston Torok, HAS, Institute of Psychology
Answer by duck · Feb 10, 2010 at 08:26 AM
Unity has a very simple built-in method of saving small amounts of data to the local machine using the PlayerPrefs class, which saves you from having to deal with explicitly reading and writing files. For example:
// to save an integer value PlayerPrefs.SetInt("Player Score", 10);
// to retrieve the integer value print (PlayerPrefs.GetInt("Player Score"));
And the commands are very similar for floats and strings:
PlayerPrefs.SetFloat("Player Lap Time", 29.3); print (PlayerPrefs.GetFloat("Player Lap Time"));
PlayerPrefs.SetString("Player Name", "Duck");
print ("Welcome back, " + PlayerPrefs.GetString("Player Name") + "!");
You could use this to write the x,y,z values of the player position and rotation.
You can also query the player prefs data to see whether a certain key has been set, using PlayerPrefs.HasKey, and delete keys using either PlayerPrefs.DeleteKey or PlayerPrefs.DeleteAll.
PlayerPrefs is only small amounts of data if you're using the web player, in which case it's 1$$anonymous$$B. Otherwise there are no limits. But yes, this is generally preferable to using System.IO since it's automatically cross-platform, and easier to deal with.
I figured for psychology experiments you usually want everything in text files at the end to convert to Excel and lateron convert to statistical software for analysis. So I went for text files and System.IO. Not sure how the process with PlayerPrefs would be.
Sure, if you want to create files in a format for other apps to read, using FileIO makes more sense. Using playerprefs only makes sense if the data is relatively small, and only needs to be read back by the unity file which wrote it.
How about arrays?
var pets = ["cats", "dogs", "fish"]; PlayerPrefs.SetArray(pets);
Would that work?
Answer by Sebas · Feb 10, 2010 at 08:13 AM
Sounds very similar to my work (also psychology with focus on navigation). I load and save all my data from text files using streamwriter link.
Should look something like this.
function Update () {
if (Input.GetButtonDown ("Fire1")) { var fileName : String;
fileName = "C:\\test.txt";
var stream = System.IO.StreamWriter(fileName);
stream.WriteLine("test"); stream.Close();
} }
Load and save your positions in a textfile. So basically, load player position, set his transform accordingly, enable him walking through the environment and when he clicks or reaches his destination, just save and repeat the whole process. If you need more details, I am basically doing something very similar.
Drop me an email with what you do for your research (sebastian DOT koenig AT canterbury DOT ac DOT nz).
By the way, I utilized your script above in my experimental task and it works beautifully. Thank you!
Answer by Agoston Torok · Feb 10, 2010 at 09:51 PM
Thanks to you I'm using the streamwriter, hope that this would work. And as you mentioned i prefer text files as output for further statistics, so the PlayerPrefs wouldn't be enough. I feel, that it needs some hours/days to finish it, but i will inform you about the progress os success. Thank you all!
Agoston Torok
Answer by Agoston Torok · Feb 18, 2010 at 11:03 AM
Problem solved, it's working very well. Thank you very much!
Answer by Neuropsyched · Sep 16, 2010 at 03:17 AM
This is probably a dead thread but I just started in on using Unity for my experiments and wanted to talk to a few fellow psychologists about their experiences.
I just finished working up a reaction time experiment I'll be implementing on the iPad and am concerned about the accuracy of the times that are generated. I did a very VERY low tech check by comparing the Unity generated times with those of a stopwatch and the times seemed to check out. Specifically, the events in Unity and the stopwatch were started at about the same time and basically matched (assuming that the .04 second difference was due to human error in the synchronization). What are your experiences with RT? Feel free to contact me at dgs45 AT drexel DOT edu