Transform.position is rounded to 0.1

Hiyas when I save a position to XML with:

if (File.Exists(filepath))
        {
            xmlDoc.Load(filepath);

            XmlElement elmRoot = xmlDoc.DocumentElement;

            elmRoot.RemoveAll(); // remove all inside the transforms node.
            Transform[] Container = GameObject.Find(GameobjectContainer).GetComponentsInChildren<Transform>();
            int i = 0;
            foreach (Transform child in Container)
            {
                if (child != Container[0])
                {
                    i++;
                    XmlElement elmMain = xmlDoc.CreateElement("GameObject");
                    XmlElement elmID = xmlDoc.CreateElement("ID");
                    elmID.InnerText = i.ToString();

                    XmlElement elmDat = xmlDoc.CreateElement("data");
                    elmDat.InnerText = child.name;

                    XmlElement elmPos = xmlDoc.CreateElement("position");
                    elmPos.InnerText = child.transform.position.ToString();


                    XmlElement elmRot = xmlDoc.CreateElement("rotation");
                    elmRot.InnerText = child.transform.rotation.eulerAngles.ToString();

                    XmlElement elmSca = xmlDoc.CreateElement("scale");
                    elmSca.InnerText = child.transform.localScale.ToString();

                    elmMain.AppendChild(elmID);

                    elmMain.AppendChild(elmDat);
                    elmMain.AppendChild(elmPos);
                    elmMain.AppendChild(elmRot);
                    elmMain.AppendChild(elmSca);

                    elmRoot.AppendChild(elmMain);
                }
            }
            xmlDoc.Save(filepath); // save file.
        }

the position, rotation and localscale is rounded to 0.1 - what’s the best way to avoid this?

You are saving the Vector3 values by simply using .ToString(). This is only a visual representation of the data. You should be saving the x, y and z values separately - they will store the entire float values for each component of the Vector3.

1 Like

Wasn’t aware of this :slight_smile: thank you

XmlElement elmPos = xmlDoc.CreateElement("position");
                    XmlElement elmPosX = xmlDoc.CreateElement("X");
                    elmPosX.InnerText = child.transform.position.x.ToString();
                    XmlElement elmPosY = xmlDoc.CreateElement("Y");
                    elmPosY.InnerText = child.transform.position.y.ToString();
                    XmlElement elmPosZ = xmlDoc.CreateElement("Z");
                    elmPosZ.InnerText = child.transform.position.z.ToString();

is working