- Home /
How do I get the upper-right corner screen position of a model?
I have a model rendered by a GameObject. I wish to add a small 2D icon at the upper right corner of the model. How do I get the 2D screen position of that point? (I'll be using OnGUI to render the icon in a label once I find the position)
Answer by DanjelRicci · Jun 11, 2011 at 07:52 AM
It's actually very easy. Use: Camera.WorldToScreenPoint
in your object to obtain its position on the screen. You can find the documentation here.
Well, that would give you the center point of your model... not the upper right corner
Renderer.bounds will give you the axis aligned bounding box in world space. you could use WorldToScreenPoint with the corners and pick the upper right.
http://unity3d.com/support/documentation/ScriptReference/Renderer-bounds.html
Bunny83, once you got the (X,Y) coordinates of the center point with Camera.WorldToScreenPoint, you can simply draw the icon at (X+A,X+B) where A and B are the right values to make the icon appear at the top-right corner.
This will work well enough in every situation, unless you need an extremely precise position.
In that case, here's another good solution.
Create another GameObject (wich we can call "iconPos") and make it children of the GameObject wich you want to draw the icon on. Then, adjust "iconPos" transform's position in order to place it at the top-right corner of your parent object. Then, use Camera.WorldToScreenPoint to get the screen position of iconPos and draw the icon directly there.
@$$anonymous$$r Drayton, how do I find the right A and B value?
@Extrakun you should try yourself, A and B depends on the object visual size and screen proportions. Let's say that (X+70,Y+70) will shift the icon towards that top-right corner, but you have to change A and B to make the icon appear ON the top-right corner.
Your answer
Follow this Question
Related Questions
How To Open GUI Editor? 1 Answer
networkview + GUI 2 Answers
Why are all my GUITexture elements semi-transparent? 0 Answers
How to display textures that are in an array? 1 Answer
gui text button dont work 1 Answer