The question is answered, right answer was accepted
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];
}
}
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;
}
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 ...
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 aVector3
( 3 x float, XYZ angles in degrees, simple )transform.rotation
is aQuaternion
( 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);
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