Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 omatase · Aug 31, 2014 at 02:51 AM · prefabcoordinatesscreentoworldpoint

Help aligning instantiated prefab in the world view

I have a prefab that I'm dynamically instantiating. When I do it asks for a Vector3. I had been struggling with how to position it correctly using pixel offsets but following some advice I received here I have been able to learn how the screen to world points is supposed to work and the "screen" coordinate system in general.

However, although getting my prefab centered works fine and seems to make sense, when I use what I know about centering it to try and place it flush with the top of the screen I have some unexpected results. The prefab is the rectangular box near the top of the screen shown in this image below.

alt text

Here is the code I'm using.

 int worldUnits = 100;
 
 Instantiate(teamView, Camera.main.ScreenToWorldPoint(new Vector3((Screen.width / 2), (Screen.height) - ((teamView.renderer.bounds.size.y * worldUnits) / 2), gameObject.layer)), Quaternion.identity);

Appreciate any help!

not flush.jpg (39.9 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Aug 31, 2014 at 03:59 AM

It is easiest to get a world object placed correctly if you setup your anchor correctly. For this, I would place an empty game object centered horizontally and right at the top of the prefab. Then I'd make the visible game object a child of the empty game object. By doing this you've essentially change the pivot point from wherever it was placed (usually the center) to the middle top. Then to place it you can just do:

   instantiatedPrefab.transform.position = Camera.main.ViewportToWorldPoint(new Vector3(0.5f, 1.0f, depth));

Where 'depth' is the distance you want the prefab away from the camera plane.

Comment
Add comment · Show 2 · 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 omatase · Aug 31, 2014 at 04:24 PM 0
Share

Thanks @robertbu I think I had a suggestion a few weeks ago from someone that I attach an empty game object to help with positioning. Is this a pretty common practice then in Unity? Like a de facto standard to solve this kind of problem? I can do this to align this but I'd still really like to find out what it is I'm missing about positioning in screen space that is keeping me from getting this to line up correctly without this workaround.

avatar image robertbu · Aug 31, 2014 at 06:56 PM 0
Share

Empty game objects are frequently used for this kind of thing. If something is modeled, it is a bit better to put the pivot in the 'right' place, but a empty game object can usually solve this problem.

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Understanding Unity's coordinate system 3 Answers

Need help putting an object at a point based on screen coordinates 1 Answer

My objects instantiate at a strange z coordinate 0 Answers

World coordinate changes when update prefab 0 Answers

how can i get the position of the objects in a instantiated prefab? 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