- Home /
how to get relative position of a game object with respect to parent
i have a panel that contains an image, and that panel is child of some other game object. i need to calculate the position of panel with respect to parent object. for now as its local position is 0,0 but globally it starts at almost half of the width of the parent. how can i get the relative position of this panel game object? or can check where in the whole plane/scene this game object resides?
Answer by hexagonius · Feb 26, 2015 at 03:28 PM
Check out transform.TransformPoint, it will transform whatever vector3 (position) to world space
yes i tried this one but it return me the position of -0.3,1.05,-9.99 something like that. may b i need to set the relative position in the inspector? because it returns me the same values as shown inspector... but i want that in inspector as well child elements must have a relative position to parent how can i set that? also in the whole plane my object starts at the half of the width and end at the bottom but these values of -0.3,1.05,-9.99 does not seems to be the actual location of object with respect to the whole plane?
On a child object the values in the transform in the inspector are in Local Space. This takes the centre of the parent to be 0.
how can i set it relative to the global space? my parent object is at 0 and child is at -0.3,1.05,-9.99 but i want the position of child object relative to the whole space?
i am using this script attatched to the camera to get the position Vector3 ts = transform.TransformPoint(titleImage.transform.localPosition.x, titleImage.transform.localPosition.y,titleImage.transform.localPosition.z); title image is the panel game object whose position i need in the global space. what i am doing wrong?
here is the hierarchy i am using, may b i need to modify the positions of child object to make it relative to the parent or i cant get it relative to the camera?
Answer by DiegoSLTS · Feb 27, 2015 at 02:05 PM
If you want the position of a UI object relative to it's parent don't use transform.localPosition. UI objects (I assume you're using the new UI on Unity 4.6) have a Rect Transform. You can get the component with GetComponent or cast the transform to a RectTransform.
Once you have the RectTransform you can access the values you setup in the inspector using some of this things: http://docs.unity3d.com/ScriptReference/RectTransform.html
Depending on the anchor reference you used those values mean different things, but maybe anchoredPosition is enough (it'll return the values for "pos x" and "pos y" fields on the inspector)
i can get the values shown in inspector but i need the position of image relative to whole screen.how can i get that? i currently used this, but it also does not return me the correct position
PointerEventData pointer = new PointerEventData(EventSystem.current); pointer.position = ScreenCaptureCamera.GetComponent().WorldToScreenPoint(new Vector3(TitleImage.GetComponent().pivot.x,TitleImage.GetComponent().pivot.y,camera.nearClipPlane));
float x =pointer.position.x- (TitleImage.GetComponent<RectTransform> ().sizeDelta.x / 2) ;
float y =pointer.position.y- (TitleImage.GetComponent<RectTransform> ().sizeDelta.y / 2) ;
Vector2 newposition= ScreenCaptureCamera.GetComponent<Camera>().WorldToScreenPoint(new Vector3(x,y,camera.nearClipPlane));
I'm not sure how to get the position relative to the screen, I've never done that, but in your original question you asked about it's position relative to it's parent, this is a different questions.
If you managed to get the position relative to it's parent I guess you can iterate on the parents hierarchy until you reach a game object with a Canvas component. You get the relative position between an image an it's parent, then that parent relative position with it's parent, and so on. Then the Image position would be a sum of the relative positions between all the parents.
Your answer
Follow this Question
Related Questions
GameObject change Position after game started 1 Answer
Scrolling Sprite Object 0 Answers
Get position for each Gameobject (in array) ? 0 Answers
Instantiate as a child at position 2 Answers