- Home /
Instantiated object should be rotated by 90 but it is only 0.7
Hi!
I have this code to spawn objects from a saved file and when I spawn the object it should have a rotation of 90 but it is only 0.7. According to the Debug.Log I made the problem is where I am saving the object. Here's my code:
For saving:
mapData.tileData.Add(new TileData(aObj.GetComponent<TileManager>().tileID, aObj.transform.position.x, aObj.transform.position.y, aObj.transform.position.z,
aObj.transform.rotation.x, aObj.transform.rotation.y, aObj.transform.rotation.z));
For loading:
temporaryObject.transform.eulerAngles = new Vector3(od.xRot, od.yRot, od.zRot);
All this code is just for the rotation since the other is private.
I can't really see anything wrong with this code. The save code just adds a tile to a list.
So if you know what is wrong, please tell me!
Thanks in advance!
Answer by maccabbe · Apr 18, 2015 at 05:07 PM
In the first block of code you are using transform.rotation which is a quaternion (and has a w, x, y, z) but only save the x, y, z parts as though it was a eularangle and later load the x, y, z parts as though it was a eulerangler even though it is 3 parts of a quaternion. You can either use Quaternion.eularAngles to convert the rotation from quaternion to eularAngles
mapData.tileData.Add(new TileData(aObj.GetComponent<TileManager>().tileID, aObj.transform.position.x, aObj.transform.position.y, aObj.transform.position.z,
aObj.transform.rotation.eulerAngles.x, aObj.transform.rotation.eulerAngles.y, aObj.transform.rotation.eulerAngles.z));
or just get the rotation in eulerAngles using Transform.EularAngles
mapData.tileData.Add(new TileData(aObj.GetComponent<TileManager>().tileID, aObj.transform.position.x, aObj.transform.position.y, aObj.transform.position.z,
aObj.transform.eulerAngles.x, aObj.transform.eulerAngles.y, aObj.transform.eulerAngles.z));
Thank you for this! I can't believe I didn't use eularAngles! I'm such a fool.
Your answer
![](https://koobas.hobune.stream/wayback/20220613193433im_/https://answers.unity.com/themes/thub/images/avi.jpg)