Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Alnae · Jan 18, 2012 at 10:45 PM · javascriptpositionplayerplayerprefssaving

PlayerPrefs Saving Player's Position Java Script

I've been trying to make a code where every 5 seconds my player's position is saved. Then after turning off the game and resuming it the player will be in the last position saved. I've looked at different tutorials and even the responds from questions like this one. None of them have worked for me for various reasons. (Code is outdated, Not Java, etc) I'm unfamiliar with coding because I haven't worked with Unity for some time.

So I'm asking for a Java Script code that solves this issue, or even a tutorials.

Thank you in advance. :]

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by kevork · Jan 18, 2012 at 11:10 PM

On a script attached to the player, when you want to save use:

 PlayerPrefs.SetFloat("PlayerX", transform.position.x);
 PlayerPrefs.SetFloat("PlayerY", transform.position.y);
 PlayerPrefs.SetFloat("PlayerZ", transform.position.z);

and when you want to restore use:

 Vector3 newPosition = Vector3.zero;
 newPosition.x = PlayerPrefs.GetFloat("PlayerX");
 newPosition.x = PlayerPrefs.GetFloat("PlayerY");
 newPosition.x = PlayerPrefs.GetFloat("PlayerZ");
 transform.position = newPosition;

There are many other significantly better ways to achieve your goal.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jem_Stone · Oct 26, 2015 at 01:40 AM 0
Share

Would it work like this

function Start () { Vector3 newPosition = Vector3.zero; newPosition.x = PlayerPrefs.GetFloat("PlayerX"); newPosition.x = PlayerPrefs.GetFloat("PlayerY"); newPosition.x = PlayerPrefs.GetFloat("PlayerZ"); transform.position = newPosition; } function Update () { PlayerPrefs.SetFloat("PlayerX", transform.position.x); PlayerPrefs.SetFloat("PlayerY", transform.position.y); PlayerPrefs.SetFloat("PlayerZ", transform.position.z); }

avatar image
0

Answer by seth_slax · Oct 26, 2015 at 02:25 AM

Look for PlayerPrefsX in Google. You should come across an extended version of PlayerPrefs (created by the Unity team, I believe) that allows you to save practically all types of variables in the same way you use PlayerPrefs, including Vector3's.

Also, parts of the above script are in C# syntax. A JS equivalent of what you wrote would be:

 function Start () { 
 var newPosition : Vector3 = Vector3.zero; 
 newPosition.x = PlayerPrefs.GetFloat("PlayerX"); 
 newPosition.y = PlayerPrefs.GetFloat("PlayerY");
 newPosition.z = PlayerPrefs.GetFloat("PlayerZ"); 
 transform.position = newPosition; 
 } 
 
 function Update () { 
 PlayerPrefs.SetFloat("PlayerX", transform.position.x); PlayerPrefs.SetFloat("PlayerY", transform.position.y); PlayerPrefs.SetFloat("PlayerZ", transform.position.z); 
 }
 
 //PlayerPrefsX equivalent would be:
 
 function Start () { 
 var newPosition : Vector3 = PlayerPrefsX.GetVector3("PlayerPosition"); 
 transform.position = newPosition; 
 } 
 
 function Update () { 
 PlayerPrefsX.SetVector3("PlayerX", transform.position);
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Oct 26, 2015 at 08:06 AM 0
Share

You should make sure you have the value stored or assign the default value if your player should start somewhere else than origin.

avatar image Jem_Stone · Oct 26, 2015 at 12:17 PM 0
Share

yay it worked thanks

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Camera changes position? 1 Answer

Change Players Position On Scene Start 0 Answers

Saving with PlayerPrefs? How it works? 1 Answer

Camera rotation around player while following. 6 Answers

Player Prefs saving position Script assistance. 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges