- Home /
Unable to set Rotation
I've written a basic script to set the position and rotation of a gameobject with PlayerPrefs. In short, the engine is recognizing the correct values, but when it 'sets' them, the gameObject is always at 0,0,0. No errors or any other warnings.
These are apart of the same script, the save part is wrapped in the Update function. The print statement output the correct values, and the position is right. But the rotation is always at 0,0,0.
void Start ()
{
//source = this.GetComponent<AudioSource>();
print("Setting player pos: " + PlayerPrefs.GetFloat("PositionX") + "," + PlayerPrefs.GetFloat("PositionY") + "," + PlayerPrefs.GetFloat("PositionZ"));
print("Setting player rot: " + PlayerPrefs.GetFloat("RotationX") + "," + PlayerPrefs.GetFloat("RotationY") + "," + PlayerPrefs.GetFloat("RotationZ"));
if (PlayerPrefs.GetFloat("PositionX") != 0f && PlayerPrefs.GetFloat("PositionY") != 0f && PlayerPrefs.GetFloat("PositionZ") != 0f) //ensure a value has been saved
{
this.gameObject.transform.position = new Vector3(PlayerPrefs.GetFloat("PositionX"), PlayerPrefs.GetFloat("PositionY"), PlayerPrefs.GetFloat("PositionZ"));
//this.gameObject.transform.eulerAngles = new Vector3(PlayerPrefs.GetFloat("RotationX"), PlayerPrefs.GetFloat("RotationY"), PlayerPrefs.GetFloat("RotationZ"));
this.gameObject.transform.localRotation = Quaternion.Euler(0,90f,0);
}
}
if(Input.GetKeyDown(KeyCode.T))
{
PlayerPrefs.SetFloat("PositionX", this.gameObject.transform.position.x);
PlayerPrefs.SetFloat("PositionY", this.gameObject.transform.position.y);
PlayerPrefs.SetFloat("PositionZ", this.gameObject.transform.position.z);
PlayerPrefs.SetFloat("RotationX", this.gameObject.transform.localEulerAngles.x);
PlayerPrefs.SetFloat("RotationY", this.gameObject.transform.localEulerAngles.y);
PlayerPrefs.SetFloat("RotationZ", this.gameObject.transform.localEulerAngles.z);
print("Setting Rot: " + PlayerPrefs.GetFloat("RotationX") + "," + PlayerPrefs.GetFloat("RotationY") + "," + PlayerPrefs.GetFloat("RotationZ"));
// source.clip = save;
source.PlayOneShot(save, 1);
PlayerPrefs.Save();
}
...you have the rotation loading code commented out. Of course it won't load.
Even if un-commented, the next line ALWAYS sets the rotation to (0,90,0). Will need to remove that too.
Also: Note that this is only going to work for a SINGLE GameObject, since your PlayerPrefs wont be able to distinguish between one objects use of playerPrefs.SetFloat("RotationZ",..) and another's. If used for more than one object, might want to use some ID number to distinguish the keys.. eg
PlayerPrefs.SetFloat("RotationZ for " + GetInstanceID().ToString(), ..... );
Also, can you clarify WHICH print statement outputs (0,0,0)?
You can also check that no active animator is attached to the object, with active null animation.
Answer by Anupam13 · Feb 03, 2017 at 08:59 PM
Try using- gameobject.transform.rotation = Quaternion.Euler (new Vector3 (a, b, c));