Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Ryebread5 · Mar 04, 2017 at 09:33 AM · c#camerarotationplayerprefssaveload

Save player rotation in playerprefs c#

I made it so I can save the players position, but is there a way to save the players rotation?

I am attempting a small RPG game and would like to have it when logged out it saves the player position AND rotation. I've looked all over but everyone just keeps talking about the position and not the camera rotation. I attempted to set the rotation the same way as the position along with a debugger to show if it is being saved. When I click the save key it shows in the console the rotx that was saved but when I try to load it, the rotation doesn't change.

I create a Transform to set the player as (This lets me save the camera rotation (I think))

 public Transform playerRot;

I save the position and rotation

 void Save()
 {
     //Sets Position
     PlayerPrefs.SetFloat ("x", transform.position.x);
     PlayerPrefs.SetFloat ("y", transform.position.y);
     PlayerPrefs.SetFloat ("z", transform.position.z);
     //Sets Rotation
     PlayerPrefs.SetFloat ("rotx", playerRot.rotation.x);
     PlayerPrefs.SetFloat ("roty", playerRot.rotation.y);
     PlayerPrefs.SetFloat ("rotz", playerRot.rotation.z);
     Debug.Log (PlayerPrefs.GetFloat("rotx"));
 }

Than i just take the position and rotation and load it

 void Load()
 {
 if(PlayerPrefs.GetFloat("x") != null && PlayerPrefs.GetFloat ("y") != null && PlayerPrefs.GetFloat ("z") != null)
 {
     //Gets and Changes Position
     transform.position = new Vector3(
         PlayerPrefs.GetFloat ("x"),
         PlayerPrefs.GetFloat ("y"),
         PlayerPrefs.GetFloat ("z"));
 
     //Gets and Changes Rotation
     playerRot.rotation = Quaternion.Euler(
         PlayerPrefs.GetFloat("rotx"),
         PlayerPrefs.GetFloat("roty"),
         PlayerPrefs.GetFloat("rotz"));    
 }
Comment
Add comment · Show 3
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 Ryebread5 · Mar 04, 2017 at 09:39 AM 0
Share

I just changed the

 playerRot.rotation = Quaternion.Euler(
     PlayerPrefs.GetFloat("rotx"),
     PlayerPrefs.GetFloat("roty"),
     PlayerPrefs.GetFloat("rotz"));

and changed it to

 playerRot.rotation = new Quaternion(
     PlayerPrefs.GetFloat("rotx"),
     PlayerPrefs.GetFloat("roty"),
     PlayerPrefs.GetFloat("rotz"),
     playerRot.rotation.w);

And when I click the load key it loads the correct saved rotation but that very quickly snaps back to what the rotation was before I loaded. Any idea on how to not make it snap back?

avatar image hexagonius Ryebread5 · Mar 04, 2017 at 12:07 PM 0
Share

you should save w the same way you do save the other values.
If it's only Load that gets called there's no reason for any back snapping. $$anonymous$$aybe the problem is another code that alters the rotation after setting it your way.

avatar image Ryebread5 hexagonius · Mar 06, 2017 at 05:35 AM 0
Share

I am using unity's character controller script. I've looked in there for camera rotation but it only has the cameras position

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by KoenZyx · Apr 08, 2017 at 07:30 PM

@Ryebread5 Just managed to fix this in my own project. In the Start() of FirstPersonController it says m_MouseLook.Init(transform , m_Camera.transform);

After that the Camera is updated each frame with the MouseLook class. So either postpone that line until you've set the Camera.transform to your saved quaternion (this is what I did) or use a method in MouseLook to set your saved quaternion. MouseLook.Init() might be what you would use there as well but I haven't tested that exactly.

If you want to create that method yourself or if you're making other modifications to Standard Asset scripts (like the first solution I suggested) you should probably just make a copy and use that instead. When you mix your own code with imported code you'll lose track of what goes where. You may not care, you may have already done so, just saying.

When I've loaded the camera rotation I call this method in my CustomFirstPersonController where you can see by the constructor of MouseLook that it is actually my CustomMouseLook.

 public void Ready()
 {
     m_MouseLook.Init(transform , m_Camera.transform, smootheMouse, smootheMouseBy);
     ready = true;
     turnedOn = true;
 }

You don't need my code for any of this, it's just an example. Try messing with the smooth and smoothTime values built into MouseLook, it's fun. Let me know if you run into any issues!

P.S. First time posting on unity3d, woohoo!

P.P.S. Figuring this out + writing this really made me feel like the MouseLook class should have been a component on the camera, but I'm probably wrong.

Comment
Add comment · 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
0

Answer by AurimasBlazulionis · Mar 04, 2017 at 06:17 PM

Change

 PlayerPrefs.SetFloat ("rotx", playerRot.rotation.x);
 PlayerPrefs.SetFloat ("roty", playerRot.rotation.y);
 PlayerPrefs.SetFloat ("rotz", playerRot.rotation.z);

to:

 PlayerPrefs.SetFloat ("rotx", playerRot.rotation.eulerAngles.x);
 PlayerPrefs.SetFloat ("roty", playerRot.rotation.eulerAngles.y);
 PlayerPrefs.SetFloat ("rotz", playerRot.rotation.eulerAngles.z);

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 Ryebread5 · Mar 09, 2017 at 03:04 AM 0
Share

Still doesn't work. I am able to save and load the rotation of the player but whenever I load it it shows the saved rotation than snaps back to what the rotation was before I load it. I am using unity's character controller script/player that can be imported when starting. I've looked into the script for camera rotation but didn't find anything in there. Any ideas about this?

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

335 People are following this question.

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

Related Questions

Roll a ball, treating where the camera is rotating as forward? 0 Answers

Camera rotation and zooming 1 Answer

my camera is rotating on Z axis when i just set it on X and Y 0 Answers

Free Look Camera Rig Changing Rotation 1 Answer

How do I control the camera with my mouse? 0 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