Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gull · Feb 26, 2015 at 01:34 PM · gameobjectpositionrelative position

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by hexagonius · Feb 26, 2015 at 03:28 PM

Check out transform.TransformPoint, it will transform whatever vector3 (position) to world space

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gull · Feb 27, 2015 at 04:17 AM 0
Share

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?

avatar image meat5000 ♦ · Feb 27, 2015 at 04:19 AM 0
Share

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.

avatar image gull · Feb 27, 2015 at 04:23 AM 0
Share

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?

avatar image gull · Feb 27, 2015 at 04:34 AM 0
Share

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?

avatar image gull · Feb 27, 2015 at 04:40 AM 0
Share

alt text

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?

relative-position.jpg (151.1 kB)
Show more comments
avatar image
0

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)

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gull · Mar 03, 2015 at 07:13 AM 0
Share

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));
avatar image gull · Mar 03, 2015 at 09:34 AM 0
Share

alt text

image.jpg (50.9 kB)
avatar image DiegoSLTS · Mar 08, 2015 at 04:34 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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

problem moving a prefab object in script (c#) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges