Setting position of child transform, relative to parent

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.

Can use transform.localPosition:

8 Likes

Thanks, mgear! Using localPosition did the trick in the end.

1 Like

thanks! That worked.

Thanks
It helped a lot even in 2021

2 Likes

it works :wink:

Worked for me too!

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. :slight_smile:

1 Like

helped me too!

A like suffices here rather than resurrecting a thread.

Let’s close this thread that still has nothing to do with physics.