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 vladdrummer · Feb 15, 2014 at 08:43 PM · positionscreen

How can I hang something in the corner of the screen?

Hi.

I'm learning Unity for 2 weeks only so I don't know many things. I need to have something in a left corner - a picture of a coin (Sprite) and 3D-text child to it with the number of coins I collected (because gui text uses viewport and cannot be parented).

So, 1st thing I tried is to have a MainCamera parent to my coin, but, of course, when I try different screen sizes - it goes offScreen.

So I made a script in void Awake() to change coordinates of a coin using camera.ScreenToWorldPoints .. but as it has being parented by a camera - it goes off the screen.

Then I thought may be I don't need a parent camera , so I wrote changing of coordinates to void Update() using same ScreenToWorldPoints. But , using it on Update make my sprite jumping around sometime and visually it looks bad.

So, please, help me, how can I hang some View (in my case, Sprite of coin with 3D text over it) so it would constantly hang in the left upper corner?

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
2

Answer by ankush_Kushwaha · Feb 16, 2014 at 12:05 PM

Why dont you use NGUI sprites with anchor which stick your object in corner on every screen size. Your Task will definitely be very easy by using NGUI sdk. It worked for me like a charm.

Comment
Add comment · 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
0

Answer by erick_weil · Feb 15, 2014 at 08:55 PM

set the coin at parent to the camera, and try to set the position of the coin like this :

    var valor:float = transform.localPosition.z;
    var valor2:float = transform.localPosition.x;
 // percentage is values between 0 and 1;
    var percentagex : float=-1;
    var percentagey : float=1;
 // this only works to a perspective camera with field of view marked to 60
     transform.localPosition.x = (((Screen.width*percentagex)/Screen.height)/2)*valor;   
     transform.localPosition.y = (((Screen.height*percentagey*2)/Screen.width)/2)*valor2;   

   
 

this will be fixed at a relative Screen position, scale and aproximate the coin (put the coin at upper border manually).

this works fine to me, with a perspective camera;

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 vladdrummer · Feb 15, 2014 at 09:13 PM 0
Share

your code is not executable, you also included height to localPosition.x calculations. And Screen.width and Screen.heights are in pixels when our coordinates are in World points

avatar image erick_weil · Feb 15, 2014 at 10:00 PM 0
Share

I updated the code to the coin adapt to the left border, set then at upper border manually. if you want somthing at right border change percentage to -1;

avatar image vladdrummer · Feb 15, 2014 at 10:04 PM 0
Share

Cannot modify a value type return value of `UnityEngine.Transform.localPosition'. Consider storing the value in a temporary variable

avatar image erick_weil · Feb 15, 2014 at 10:26 PM 0
Share

i am sure thats can be modified, I tested and worked very fine to me, I am using javascript, and this code is into the update function

updated the code a new time, now snap the y posi too

avatar image vladdrummer · Feb 15, 2014 at 10:34 PM 0
Share

thats an error in Unity. it's because it needs a Vector3 for variable. I tryed to do this:

 void Update(){
         float valor = transform.localPosition.z;
         float valor2 = transform.localPosition.x;
         // percentage is values between 0 and 1;
         float percentagex=-1;
         float percentagey=1;
         // this only works to a perspective camera with field of view marked to 60
         Vector3 newpos= new Vector3 ((((Screen.width*percentagex)/Screen.height)/2)*valor, (((Screen.height*percentagey*2)/Screen.width)/2)*valor2,.3f);
         transform.localPosition=newpos;
     }

but it still doesn't change a position

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

20 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

Related Questions

how to have an object positioned relative to the screen(like a button) 1 Answer

How to get the screen position in an unlit shader. 1 Answer

Is there a way to lock GUI buttons to a screen position insted of a pixel defined position so it will show up no matter the resolution of the screen? 0 Answers

screen position of element in layout 1 Answer

Change position and scale of objects according to mobile screen size 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