Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Setting position of child transform, relative to parent

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

  1. moosefetcher

    moosefetcher

    Joined:
    Sep 23, 2015
    Posts:
    67
    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:
    5,538
  3. moosefetcher

    moosefetcher

    Joined:
    Sep 23, 2015
    Posts:
    67
    Thanks, mgear! Using localPosition did the trick in the end.