Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 votagus · Dec 13, 2016 at 02:23 AM · unity 5positionui imagepositioningcenter

Proper way to center a game object into an UI image?

Hi guys, I'm currently trying to center a game object into an UI image, the thing is, I can't figure out how to do it, I'll try to explain with screen captures.

I have an image that acts as the container of the game object, not in the hierarchy though, and that container is not in the center of the screen but slightly more at the right. What I want is to place that game object in the center of the image. alt text

If I put the game object in the position of the rect transform of the image then it gets centered as you can see below.

 var container = GameObject.FindGameObjectWithTag("AvatarContainer").GetComponent<RectTransform>();
 gameobject.transform.position  = container.position;

But it does not matches from the camera view, it is even outside of the camera space. How can I get the correct position?

Thanks guys!

alt text

f.jpg (262.6 kB)
1.jpg (207.1 kB)
Comment
Add comment · Show 1
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 Artaani · Dec 18, 2016 at 04:39 PM 0
Share

You managed to solve your question?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Artaani · Dec 13, 2016 at 08:02 AM

Use Camera.ScreenToWorldPoint

Example:

 // Let's find a position (it will be position in pixels on the screen)
 Vector3 containerPosition = GameObject.FindGameObjectWithTag ("AvatarContainer").GetComponent <RectTransform> ().position;
 
 // Project screen position to world position using specific camera (main camera in this case)
 float DesireDistanceFromCamera = 5;
 gameobject.transform.position = Camera.main.ScreenToWorldPoint (new Vector3 (containerPosition.x, containerPosition.y, DesireDistanceFromCamera));

Upd: Link on project folder (Unity 5.5)

Comment
Add comment · Show 6 · 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 votagus · Dec 13, 2016 at 01:25 PM 0
Share

Thanks for your answer, but it does not work, I already tried with ScreenToWorldPoint before asking here, the thing is that containerPosition is not in screen units so the resulting vector is wrong. I think the way to get the right position is to get the container position from the camera perspective and then apply that position to the game object, but how to do so?

avatar image Artaani votagus · Dec 13, 2016 at 10:43 PM 0
Share

Ah, your GUI in world space?

In that case:

 // Let's find local position of container relative to camera
 Vector3 containerWorldPosition = GameObject.FindGameObjectWithTag ("AvatarContainer").GetComponent <RectTransform> ().position;
 Vector3 containerLocalPosition = containerWorldPosition - Camera.main.transform.position;
 
 // After that, normalize this vector and place a object on this vector relative to camera
 float DesireDistanceFromCamera = 5;
 gameobject.transform.position = Camera.main.transform.position + (containerLocalPosition * DesireDistanceFromCamera);
avatar image votagus Artaani · Dec 14, 2016 at 07:18 PM 0
Share

Hi, thanks for your answer, but is not working. Here is an atlas to see what I need and what is the actual result. In the first image you can see the more or less the values that I need. In the second and third image is where the gameobject actually should be to get those values. And in the fourth image is what I get playing with DesireDistanceFromCamera. The object is indeed centered in the container, but because of the camera, which is in perspective mode, it doesn't work. As you can see in the last image, if I play with the z axis of the gameobject it will end up below the camera. I think the equation should consider also the angles, I don't know. alt text

atlas.jpg (450.9 kB)
Show more comments
Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

UI Positioning with decimal points 0 Answers

Object is not following the touch position 0 Answers

[Unity 3D] How to detect if the game object is left right from the player? 0 Answers

Different unit size on mobile and PC 1 Answer

Seemingly random bug when positioning player on scene start 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