Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Setting position of child transform, relative to parent

Discussion in 'Physics' started by moosefetcher, Aug 12, 2016.

Thread Status:
Not open for further replies.
  1. moosefetcher

    moosefetcher

    Joined:
    Sep 23, 2015
    Posts:
    74
    I have set the parent of a HUD camera's transform to be the transform of the object it's 'orbiting'. The trouble is, when I then set the position of the child transform, it is still being moved to 'global' coordinates.
    I have tested this in the Unity editor, paused the game, and the HUD camera is, indeed, a child of the parent object (it is listed under that object's hierarchy).

    hudCamera.transform.parent = hudSelectedObject.transform;
    hudCamera.transform.position = new Vector3(0, 0, -50); // this still moves the HUD camera to global position 0, 0, -50 - why?

    The parent object position is not 0, 0, 0, so it's not just that the child object position is set relative to 0, 0, 0.

    So how do I set the position of the HUD camera so it is relative to the parent transform position?
    Thanks.
     
    Last edited: Aug 12, 2016
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,268
  3. moosefetcher

    moosefetcher

    Joined:
    Sep 23, 2015
    Posts:
    74
    Thanks, mgear! Using localPosition did the trick in the end.
     
    tintin10q likes this.
  4. russisunni

    russisunni

    Joined:
    Oct 26, 2019
    Posts:
    8
    thanks! That worked.
     
  5. pjkokane21

    pjkokane21

    Joined:
    Mar 18, 2021
    Posts:
    2
    Thanks
    It helped a lot even in 2021
     
    hoonkim1126 and alex2008wxyz like this.
  6. GMBLO

    GMBLO

    Joined:
    Dec 30, 2020
    Posts:
    5
    it works ;)
     
  7. alex2008wxyz

    alex2008wxyz

    Joined:
    Jul 1, 2020
    Posts:
    5
    Worked for me too!
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,082
    Glad it worked for you guys but it's still nothing to do with physics in 2021 as it wasn't in the original 2016 post. :)
     
    TomTrottel likes this.
  9. konkord895

    konkord895

    Joined:
    Dec 11, 2021
    Posts:
    1
    helped me too!
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,082
    A like suffices here rather than resurrecting a thread.

    Let's close this thread that still has nothing to do with physics.
     
Thread Status:
Not open for further replies.