- Home /
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?
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.
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;
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
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;
Cannot modify a value type return value of `UnityEngine.Transform.localPosition'. Consider storing the value in a temporary variable
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
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
Your answer
![](https://koobas.hobune.stream/wayback/20220613133622im_/https://answers.unity.com/themes/thub/images/avi.jpg)