Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

FBX import : how to find world coordinates ?

Discussion in 'Scripting' started by markovicho, Nov 19, 2016.

  1. markovicho

    markovicho

    Joined:
    Nov 2, 2016
    Posts:
    5
    Hey :)

    I converted an obj file to fbx. It contains openstreetmap3D data like buildings, trees, streets, ...

    After importing the asset and creating a prefab of it's content I want to loop over some of the objects and place an new gameobject directly above them.

    No matter if I could imagine different ways to achieve this I am pretty much struggeling with the following behaviour which seems pretty strange to me and I would like to understand it:

    The parent element is placed at (0,0,0). All childs are also showing (0,0,0) in the inspector which is fine for me as these are the local coordinates relative to the parent. So far so good...

    But in opposite to all self created gameobjects and their childs in my world i am not allowed to get the world coordinates by accessing the position attribute. No matter which function I am using it will always return (0,0,0).

    this is my sourcecode in c#:

    Code (CSharp):
    1.       GameObject child_object = GameObject.Find("Building Commerzbank");
    2.  
    3.         Transform myTrans = child_object.transform;
    4.         Transform myParent = child_object.transform.root;
    5.  
    6.         Debug.Log("My world position = " + myTrans.position);
    7.         Debug.Log("My position relative to my parent = " + myTrans.localPosition);
    8.         Debug.Log("My parent's world position = " + myParent.position);
    9.         Debug.Log("manually transform= "+myTrans.localToWorldMatrix.MultiplyPoint3x4(myTrans.position));
    output console:

    sorry i don't get it. As it works for other objects fine the problem may be in the fbx import. Is there something i could be missing in the export/import configs to get rid of this?

    thanks in advance!!!!
     

    Attached Files:

  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Sounds like the origins of the objects are all being set to zero, and the objects are being displaced by moving the individual mesh vertices.

    Withou being familiar with your setup I can't offer you a solution. But it's a start to understanding.
     
  3. markovicho

    markovicho

    Joined:
    Nov 2, 2016
    Posts:
    5
    @BoredMormon thank you so far.

    Your hint with the displaced vertices brought me to this solution :

    Code (CSharp):
    1.         GameObject child_object = GameObject.Find("RoadJunction253");
    2.  
    3.         Vector3 [] test = child_object.GetComponent<MeshFilter>().mesh.vertices;
    4.  
    5.         Debug.Log("random mesh vertice position ="+ child_object.transform.localToWorldMatrix.MultiplyPoint3x4(test[0]));
    6.  
    the world coordinate is looking fine :)

    now it would be pretty interesting to get rid of the "workaround" and directly changing the export / import settings to achieve this automatically.

    I must say I am using Blender without any experience just to convert my obj file to fbx...

    The reason to convert to fbx was the grouping of the child elements resulting out of the obj file. With the fbx i am getting the correct hierarchy in unity.
     
    Kiwasi likes this.