- Home /
Keep GameObject on a corner of the screen
var guiCam : GameObject;
var targetPos : Vector3;
function Update ()
{
transform.localPosition = guiCam.camera.ScreenToViewportPoint(targetPos);
}
Hi, well, the thing is that I have this script attached to the gameobject that I want to keep on the corner independently of the screen resolution, and the gameobject is a child of my GUI camera (which renders all of my gui stuff), but I can't get it to work, I adjust the desire position and everything but when I go fullscreen it goes out of the screen, so it's basically not working. I don't know what I'm doing wrong. Please help! Thanks in advance.
Why are you using ScreenToViewportPoint? GameObjects don't use viewport space.
Because I saw it in another answer and I really don't know ANYTHING about all those camera functions, plus I already tried with the others but I don't quite know how to used them. I would really apreciate the right way to do it or maybe an example. Thanks.
What kind of a game object is this script attached to? A plane? A GUITexture? Some other game object? Are you trying to get the object exactly in a corner or at an edge, or can you live with approximately in the corner/edge?
It's a 3D object from blender, it's the hunger meter, I need it to be right behind the life meter which is a heart, and the hunger meter is a circular object surrounding it, the life meter is a GUITexture so it's always at the corner where it is supposed to be, but the hunger meter is not so it changes position with different resolutions
Answer by robertbu · Jun 27, 2013 at 02:10 AM
You have an issue that is solvable by using blender or by using an empty game object. That is, you need the pivot/anchor/origin of the hunger meter to be in the corner you want to anchor. You can do this by moving the pivot to the corner in blender, or by placing an empty game object in the corner and making the hunger meter a child of the empty game object. Then you can use Camera.ViewportToWorldPoint() to position the hunger meter. Note you will need to specify a distance in front of the camera to position the object.
var v3Pos = Vector3(0.0, 1.0, 0.25);
transform.position = gui.camera.ViewportToWorldPoint(v3Pos);
The above lines will place the object in the upper left corner of the screen 0.25 units in front of the camera.
Wow! I'll definately try that! I'll let you know by tomorrow :)
And if I wanted to do the same thing but with the health meter (GUITexture) so there wouldn't be any other problem?
Actually GUITextues use Viewport coordinates already, so you can set the position directly and expect it to change for different screen resolutions/ratios.
Yeah, but is not 100% accurate, in some resolutions everything's okay, but in others, the H.$$anonymous$$. is a little bit to the right according to the L.$$anonymous$$., and in others, the L.$$anonymous$$. is offset as well, so it doesn't looks so good because they have to be perfectly aligned. Is this something that can be fixed, or do I just have to live with it?
Your answer
Follow this Question
Related Questions
Collision delete 1 Answer
Set Game Object Position to Sides of a Camera 1 Answer
Game Texture - Always Face Camera 1 Answer
Rotate camera around gameobject 1 Answer
I am getting an error and cant figure out why, Help please? 1 Answer