Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Transform.position return strange value

Discussion in 'Scripting' started by SayhiUnity, Jun 20, 2017.

  1. SayhiUnity

    SayhiUnity

    Joined:
    Jan 7, 2017
    Posts:
    10
    At first, sorry for my poor English.

    I meet a strange problem about Transform.position:
    documentation said Transform.position should return Gameobject's worldspace position.

    but when i do this(my English is poor, make a picture to explain):
    headbone1.png

    This result is not correct, the real World position is: realWP1.png
    why ?

    again, sorry for my English.
     
  2. AcePilot10

    AcePilot10

    Joined:
    Aug 11, 2016
    Posts:
    34
    First off, GameObject.Find is pretty unsafe, maybe it's finding another object. I would recommend making a public field and drag the head object into the inspector field. That, or make the head have a tag. But for your problem, you're getting the local position (reference to the parent object) I believe. Try using Transform.TransformPoint();
    https://docs.unity3d.com/ScriptReference/Transform.TransformPoint.html
     
  3. Scabbage

    Scabbage

    Joined:
    Dec 11, 2014
    Posts:
    268
    Other way around mate. transform.position is indeed giving the world position, but the inspector shows the local transform. Print the transform.localPosition and it'll be the same as in the inspector.
     
  4. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,294
    transform.position returns the world position. I'm going to guess that Transform.Find("head") is picking up the Female model's head, since that probably has the same name. Don't use Transform.Find, you're pretty much always going to run into situations where things break because there's a new object with the same name.

    To figure out which object you're actually printing the position of, add it as a second parameter to your Debug.Log:
    Code (csharp):
    1. Debug.Log("headbone worldspace position is: " + headbone.transform.position, headbone);
    When you click that message in the inspector, the gameobject will get pinged in the hierarchy.


    An alternative explanation:
    With the 2D camera, it's really hard to see where the sphere is in relation to the head. Since 2D is ortographic, your sphere could really be hundreds of units away from the head!

    Alternative 3:
    Your skinned mesh renderer is messed up somehow, and offset very far from the bones. So your bones are around the origin, but the verts are pushed to where your sphere is. Not very likely, but idk.
     
  5. Wendo90

    Wendo90

    Joined:
    Mar 12, 2018
    Posts:
    14
    I had a similar problem with a transform position of a bone. In the end the wrong value came from setting the position in FixedUpdate(). When I moved the call in the Update() function, the position was correct.