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
1
Question by Tobyaxa · Feb 18, 2014 at 02:38 PM · c#resolution

How to set an objects position to the corner of the screen? (Unity 2D)

I am trying to make a HUD for my game and I have a sprite which I want to position in the upper left corner of the screen. The problem is that I want it to be in the corner for every screen resolution. I have tried programming a script but always run into problem.

P.S. I am using C#.

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
Best Answer

Answer by Aridez · Feb 18, 2014 at 03:10 PM

Add component -> rendering -> gui text/ texture. To create a HUD you should use those, they will be positioned always on a position relative to the camera so you see them on the same position all time.

edit: I had some problems with them while working on 2D, but once you manage to position them inside the camera then is just matter of positioning them where you want.

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 Tobyaxa · Feb 19, 2014 at 10:17 AM 0
Share

It seems that its not relative to the screen resolution.

avatar image Tobyaxa · Mar 06, 2014 at 12:01 PM 0
Share

nvm fixed it.

avatar image
8

Answer by robertbu · Feb 18, 2014 at 03:55 PM

Select your Sprite/Texture. Set the pivot for the sprite to the top left. Then you can use Viewport coordinates to place it in the upper left corner. Viewport coordinates go from (0,0) in the lower left to (1,1) in the upper right. So the Viewport coordinate for the upper left is (0,1). In the Start() of a script attached to the sprite put:

  transform.position = Camera.main.ViewportToWorldPoint(new Vector3(0,1,0));

Note this like of code assumes an Orthographic camera. If you are using a Perspective camera, you need to set the 'z' of the Vector3() to the distance the sprite is in front of the camera.

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 Tobyaxa · Feb 18, 2014 at 03:58 PM 0
Share

I tried this but got an error telling me I can't set the value of a transform.

avatar image Rashomonfr · Oct 13, 2014 at 08:15 AM 0
Share

you have to use the new keyword for the Vector3

 transform.position = Camera.main.ViewportToWorldPoint(new Vector3(0,1,0));

Of course the Aridez answer was the best one for your question.

avatar image parth16parikh · Jan 25, 2018 at 01:12 PM 0
Share

Thanks ! This helped me ! :)

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Developing on multiple resolutions 1 Answer

How to wait for Screen.SetResolution to finish? 3 Answers

GUI position and size in different screen resolutions 1 Answer


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