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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Inspector Position not matching position obtained in script

Discussion in 'Editor & General Support' started by TxAg03, Jul 1, 2015.

  1. TxAg03

    TxAg03

    Joined:
    May 20, 2015
    Posts:
    21
    Went to move a GameObject that contains child GameObjects, but wanted to move certain parts at different speeds/directions. Couldn't get the results I was aiming for and finally figured out why... the positions of my parts in the inspector are not the positions I obtain in my scripts using part.transform.localPosition. Because of this, I get unexpected behaviors. Attached are screenshots of my issue:

    Inspector.png
    Readout.png

    I'm running version 5.1.1f1. Any suggestions?
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Yeah, I'm really not sure why this is but I've come to accept it as part of unity. transform.localPosition seems to always return the world-space position of the transform.

    To get the local space position, try this:
    Code (CSharp):
    1. Vector3 localPos = transform.position;
    2. if (transform.parent != null)
    3. {
    4.     localPos -= transform.parent.position
    5. }
     
  3. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    If you debug log the vector3 it will show rounded values. Logging the individual axis' will show the accurate values.
     
  4. TxAg03

    TxAg03

    Joined:
    May 20, 2015
    Posts:
    21
    Thank you Gambit MSplitz. Ended up going that route because my parent GameObject wasn't at the origin. Come to find out that it was the parent/child relationship (scaling) that was causing problems.