Search Unity

GameObject position precision loss when parent to an object not at the origin.

Discussion in 'Scripting' started by Plomber, Mar 22, 2019.

  1. Plomber

    Plomber

    Joined:
    Jul 9, 2017
    Posts:
    3
    Hello everyone, bonjour !

    I use Unity 2018.3.9f1 Personal.

    I noticed something strange when programmatically parenting a GameObject to a parent that is not at the origin.
    If I set the world position of the child object, it seems that when we query the word position, this queried world position is not perfectly equal to the desired world position.

    This happens when these two methods are used :
    Code (CSharp):
    1. var childObj = GameObject.Instantiate(Resources.Load<GameObject>("ChildPrefab"), desiredChildWorldPosition, Quaternion.identity, parentObj.transform);
    And
    Code (CSharp):
    1. var childObj = GameObject.Instantiate(Resources.Load<GameObject>("ChildPrefab"), parentObj.transform, true);
    2. childObj.transform.position = desiredChildWorldPosition;
    The tests are :

    Code (CSharp):
    1.     [UnityTest]
    2.     public IEnumerator GOInstanciate_ParentTo10_10_10()
    3.     {
    4.         var parentWorldPosition = new Vector3(10f, 10f, 10f);
    5.         var desiredChildWorldPosition = new Vector3(-23.2f, 0.5833334f, -7f);
    6.  
    7.         var parentObj = new GameObject();
    8.         parentObj.transform.position = parentWorldPosition;
    9.  
    10.         var childObj = GameObject.Instantiate(Resources.Load<GameObject>("ChildPrefab"), desiredChildWorldPosition, Quaternion.identity, parentObj.transform);
    11.  
    12.         Assert.AreEqual(0f, Vector3.Distance(childObj.transform.position, desiredChildWorldPosition), "desired world position and real world position should be equal.");
    13.         Assert.AreEqual(desiredChildWorldPosition, childObj.transform.position, "child object position should be the world position set.");
    14.         yield return null;
    15.     }
    desired world position and real world position should be equal. Expected: 3,576279E-07 == 0
    Code (CSharp):
    1.     [UnityTest]
    2.     public IEnumerator TransformSetPosition_ParentTo10_10_10()
    3.     {
    4.         var parentWorldPosition = new Vector3(10f, 10f, 10f);
    5.         var desiredChildWorldPosition = new Vector3(-23.2f, 0.5833334f, -7f);
    6.  
    7.         var parentObj = new GameObject();
    8.         parentObj.transform.position = parentWorldPosition;
    9.  
    10.         var childObj = GameObject.Instantiate(Resources.Load<GameObject>("ChildPrefab"), parentObj.transform, true);
    11.         childObj.transform.position = desiredChildWorldPosition;
    12.  
    13.         Assert.AreEqual(0f, Vector3.Distance(childObj.transform.position, desiredChildWorldPosition), "desired world position and real world position should be equal.");
    14.         Assert.AreEqual(desiredChildWorldPosition, childObj.transform.position, "child object position should be the world position set.");
    15.         yield return null;
    16.     }
    desired world position and real world position should be equal. Expected: 3,576279E-07 == 0

    The difference between the queried position and the desired one tends to increase as the distance of the parent object from the origin increases.
    With a desired world position of (-23.2f, 0.5833334f, -7f) I have the following results :
    1. Parent world pos = (0,0,0) -> distance delta = 0 --- TEST OK
    2. Parent world pos = (10f,10f,10f) -> distance delta = 3,576279E-07 --- TEST KO
    3. Parent world pos = (10000f,10000f,10000f) -> distance detla = 0,0003792614 --- TEST KO

    Has anyone already encountered this ?
    To me, this seems to not be the normal behavior of world positioning as I have not found any precision in the docs (https://docs.unity3d.com/ScriptReference/Object.Instantiate.html) and I expect that the world position that I set and the one that I query are identical.

    Or maybe I am doing something wrong ?

    I noticed it because I am using this position for geometry calculations that are thus erroneous.

    I enclosed the project with the tests.
    Also available here for quick look : https://github.com/ldalzotto/Parent...ter/Assets/Test/PrentingPrecissionLossTest.cs

    Thanks.
     

    Attached Files:

  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,336
    WarmedxMints likes this.
  3. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    32-but....It shouldn't, however, it made me chuckle!
     
    Baste likes this.