- Home /
How to save players rotation in Unity? PlayerPrefs
Hello everyone,
this is my first post, so I hope I've posted it into the wrigth place. If not, excuse me. How ever, I've created a Script witch saves the players position. Writing this was a trouble, but it's working... What's missing, is the players rotation. But I don't know, how to save it. How implement it into the script. The Internet was no help after lot's of research, so hopefully you guys can help me out. How can I do it? How are you saving your Data? I am working in Unity 2018.2.
This is my script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Saving : MonoBehaviour {
public Transform transfrom;
void OnEnable()
{
if(!PlayerPrefs.HasKey("PosX"))
{
PlayerPrefs.SetFloat("PosX", transform.position.x);
}
if(!PlayerPrefs.HasKey("PosY"))
{
PlayerPrefs.SetFloat("PosY", transform.position.y);
}
if(PlayerPrefs.HasKey("PosX"))
{
transform.position = new Vector2(PlayerPrefs.GetFloat("PosX"), PlayerPrefs.GetFloat("PosY"));
}
if(PlayerPrefs.HasKey("PosY"))
{
transform.position = new Vector2(PlayerPrefs.GetFloat("PosX"), PlayerPrefs.GetFloat("PosY"));
}
}
void OnDisable()
{
if(PlayerPrefs.HasKey("PosX"))
{
PlayerPrefs.SetFloat("PosX", transform.position.x);
}
if(PlayerPrefs.HasKey("PosY"))
{
PlayerPrefs.SetFloat("PosY", transform.position.y);
}
}
}
Answer by Vega4Life · Dec 02, 2018 at 05:47 PM
Something like:
PlayerPrefs.SetFloat("RotationX", transform.eulerAngles.x);
PlayerPrefs.SetFloat("RotationY", transform.eulerAngles.y);
PlayerPrefs.SetFloat("RotationZ", transform.eulerAngles.z);
PlayerPrefs.Save();
Then get them via PlayerPrefs.GetFloat and apply them to your player transform.
Answer by SahanD · Dec 03, 2018 at 01:24 PM
PlayerPrefs.SetFloat("RotationX", transform.rotation.x);
PlayerPrefs.SetFloat("RotationY", transform.rotation.y);
PlayerPrefs.SetFloat("RotationZ", transform.rotation.z);
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Invalid floats in PlayerPrefs 3 Answers
onapplicationquit() function is not working with unity 4.6.3 (android) 3 Answers
Save Values On Closing of Game 1 Answer