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"));	
}

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);

@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.