Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed Jan 11 at 01:47 PM by Wesley21spelde for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Wesley21spelde · Jan 10 at 11:18 PM · rotationplayerloadsave data

How can i get a Rotation X.Y.Z for a save file

hey guys how are you

i am making a save and load system for my game but i am havind problems with saving the Rotation of my player in X.Y.Z i tried this but it wont rotate correct as intended.


 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class PlayerData 
 {
     public float[] position;
     public float[] rotation;
 
    
     public PlayerData (SaveLoad player)
     {
        
         position = new float[3];
         position[0] = player.transform.position.x;
         position[1] = player.transform.position.y;
         position[2] = player.transform.position.z;

         // i tried it like this..
         rotation = new float[3];
         rotation[0] = player.transform.rotation.eulerAngles.x;
         rotation[1] = player.transform.rotation.eulerAngles.y;
         rotation[2] = player.transform.rotation.eulerAngles.z;
     }
 
 }

 using System.Collections;
 using UnityEngine;
 
 public class SaveLoad : Photon.MonoBehaviour
 {
     public void SavePlayer()
     {
         SaveSystem.SavePlayer(this);
     }
 
     public void LoadPlayer()
     {
         PlayerData data = SaveSystem.LoadPlayer();
 
         Vector3 position;
         position.x = data.position[0];
         position.y = data.position[1];
         position.z = data.position[2];
 
         Vector3 rotation;
         rotation.x = data.rotation[0];
         rotation.y = data.rotation[1];
         rotation.z = data.rotation[2];
         
     }
 }


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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by andrew-lukasik · Jan 11 at 11:06 AM

Idk mate, works super fine on my machine

 using UnityEngine;
 
 public class PlayerComponent : MonoBehaviour
 {
     [SerializeField] string _saveSlot = "save-slot-name";
     [SerializeField] KeyCode _saveKey = KeyCode.F5;
     [SerializeField] KeyCode _loadKey = KeyCode.F8;
     void Update ()
     {
         if( Input.GetKeyDown(_saveKey) )
         {
             SaveData data = new SaveData{ Numbers = new float[6] };
             {
                 Vector3 pos = transform.position;
                 Vector3 rot = transform.rotation.eulerAngles;
                 
                 data.Version = 1;
                 
                 data.Numbers[0] = pos.x;
                 data.Numbers[1] = pos.y;
                 data.Numbers[2] = pos.z;
 
                 data.Numbers[3] = rot.x;
                 data.Numbers[4] = rot.y;
                 data.Numbers[5] = rot.z;
             }
             PlayerPrefs.SetString( key:_saveSlot , value:JsonUtility.ToJson(data) );
             Debug.Log($"Save Complete (data.Version:{data.Version})");
         }
         else if( Input.GetKeyDown(_loadKey) )
         {
             SaveData data = JsonUtility.FromJson<SaveData>( PlayerPrefs.GetString(_saveSlot) );
             if( data==null ) Debug.LogWarning("no save data to load");
             else if( data.Version==1 )
             {
                 Vector3 pos = new Vector3{
                     x = data.Numbers[0] ,
                     y = data.Numbers[1] ,
                     z = data.Numbers[2] ,
                 };
                 Vector3 rot = new Vector3{
                     x = data.Numbers[3] ,
                     y = data.Numbers[4] ,
                     z = data.Numbers[5] ,
                 };
 
                 transform.SetPositionAndRotation( pos , Quaternion.Euler(rot) );
                 Debug.Log($"Load Complete (data.Version:{data.Version})");
             }
             else if( data.Version==2 ) {}// etc.
             else Debug.LogWarning("data version is invalid");
         }
     }
 }
 
 [System.Serializable]
 public class SaveData
 {
     public int Version;
     public float[] Numbers;
 }
Comment
Add comment · Show 3 · 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 Wesley21spelde · Jan 12 at 12:22 AM 0
Share

hey thanks for replying. i alreddy look at jou post earlyer.. the other part of my scripts are working when i save my data rotation it saves i did a debug.log on it i got the x.y.z values but i cant get the rotation loaded becouse i cant get the vector3 working on my rotation. i see how you did it and it looks to me that the way i did it should work but i am no pro at this :D


these are for saving

rotation = new float[3]; rotation[0] = player.transform.rotation.eulerAngles.x; rotation[1] = player.transform.rotation.eulerAngles.y; rotation[2] = player.transform.rotation.eulerAngles.z;

and this for loading

    Vector3 rotation;
             rotation.x = data.rotation[0]; 
             rotation.y = data.rotation[1];
             rotation.z = data.rotation[2];
 
 Player.transform.position = position; //  <----- this works
 Player.transform.rotation = rotation;// this does not 

can you explaine to me what the problem is or how to solve it. i would be verry greatfull thanks ...

avatar image andrew-lukasik Wesley21spelde · Jan 12 at 10:15 AM 0
Share

This is because there is major difference between transform.rotation and transform.rotation.eulerAngles. Both represent rotation, yes, but in a very different format:

  • transform.rotation.eulerAngles is a Vector3 ( 3 x float, XYZ angles in degrees, simple )

  • transform.rotation is a Quaternion ( 4 x float, alien technology, incomprehensible to earthlings )


You showed that you read rotation as euler angles:

 eulerAngles[0] = player.transform.rotation.eulerAngles.x;
 eulerAngles[1] = player.transform.rotation.eulerAngles.y;
 eulerAngles[2] = player.transform.rotation.eulerAngles.z;

But you didn't show how you assign these angles back to a player.transform. I bet you made a mistake there.

Correct way is either this:

 transform.SetPositionAndRotation( pos , Quaternion.Euler(eulerAngles) );

or this:

 transform.position = pos;
 transform.rotation = Quaternion.Euler(eulerAngles);
avatar image Wesley21spelde andrew-lukasik · Jan 12 at 01:51 PM 0
Share

Thanks i got it working. i still have an other problem i noticed maybe you can help me with that as well ill send you the link to the question.

https://answers.unity.com/questions/1880805/updating-current-rotation-data-on-firstpersonlook.html

Follow this Question

Answers Answers and Comments

204 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

Related Questions

How to rotate object in sphere 0 Answers

Make character face movement direction 0 Answers

how to flip my player with rotation and not scale when the key is pressed 0 Answers

Movement/Rotation Methods Don't Work Together 0 Answers

Is it normal that saving data to file makes game freeze for a long time? 1 Answer


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