- Home /
Don't need the function anymore
How to Save/Load the rotation of my player
Hello, I'm making a 2D Platformer and I'm trying to save the rotation of the Player in Playerprefs whenever he hits the "Checkpoint" and when he dies the rotation should be loaded. The position is no problem at all but I dont get how to save/load the rotation.
If someone could point me out how to do it or give me a hint, I would be very thankfull.
Quick question before I answer this for you, why do you want to save their rotation at all? It's a 2D platformer... there's only 1 possible rotation. Is your character a ball rolling around, is the map not flat? Under what scenario could a player be at a specific checkpoint, and have more than one possible rotation?
$$anonymous$$y theory is that you don't really need to save the rotation at all, in which case I can help you simplify your whole system. If you do need it for some reason, then I can help you do that too.
In my game you have sometimes the possibility to change gravity and therefore its better to save the rotation at checkpoints, otherwise when the character dies he respawns with his last rotation and most times thats not prefert.
If saving and loading the position is not a problem, what difficulty are you facing with the rotation ? Either save the 4 components of the rotation
quaternion or the 3 components of the eulerAngles
I already tried it but nothing happened.
This is the code i was using :
// Save
PlayerPrefs.SetFloat ("rotationx", player.rotation.eulerAngles.x); PlayerPrefs.SetFloat ("rotationy", player.rotation.eulerAngles.y); PlayerPrefs.SetFloat ("rotationz", player.rotation.eulerAngles.z);
// Load
player.rotation = Quaternion.Euler( PlayerPrefs.GetFloat("rotationx"), PlayerPrefs.GetFloat("rotationy"), PlayerPrefs.GetFloat("rotationz"));
Is it possible that the error is that i have to use "transform" ins$$anonymous$$d of "rotation" ? Sorry to ask but at the moment i cant try it out.
Answer by kirito015 · May 16, 2018 at 11:21 AM
you can save the rotation as a vector if i remember you need to type transform.euler and it give you the rotation as a vector
Answer by sum1nil · May 16, 2018 at 05:23 AM
ScriptableObject(s) A class you can derive from if you want to create objects that don't need to be attached to game objects. This is most useful for assets which are only meant to store data.
https://docs.unity3d.com/ScriptReference/ScriptableObject.html
So you could use one as a datastore to hold rotation and save/read from it.
Also, this is a good tutorial on persisting objects to disk. http://catlikecoding.com/unity/tutorials/object-management/persisting-objects/
Sorry if I understand you wrong but I don't want to create objects.
I just want to save the rotation of my Player in the playerprefs when he hits a trigger.
In my case either he is standing on the ground or he is 180 degrees flipped and standing on the ceiling.
I am not sure if vectors/quaternions or any complex data type can be saved in PlayerPrefs but I believe the can be saved to a ScriptableObject.
I save/load the position of the player with playerprefs so i think there should not be a problem at all to save the rotation. The problem is I just don't get it to work.