- Home /
How to find the comparative object's position compare with game screen?
As the title, I don't know how to define the comparative object's position in script. For example, in my scene, my gameObject has the Vector3 like this: x=1000, y=0, z =500, then how can I find out the Vector2 of this gameObject compare with the screen width and screen length? Sorry for my bad English.
Yes, that's it. I know to use worldToScreenPoint but I still get some trouble that I post in Paulius's answer below :)
I think we need to see your positioning code to figure out what is going on. Note that if you are using GUI to draw items, GUI coordinates are different than screen coordinates. You have to make an extra translation.
I use GUI man :( Can we discuss in the answer below, I already posted my code there.
Answer by Paulius-Liekis · Aug 06, 2013 at 09:00 AM
Camera.main.WorldToScreenPoint (and there are a couple more helper methods like that on Camera class).
I know to use it but I still have some troubles. Here is it:
The turret on the left is alright but it seems not exactly with the turret on the right. I don't know why my menu appears below the turret.
Oops, because there are many things that's relevant in my script so I will just post my shortcut function in OnGUI method. //
var pos :Vector3 = Camera.main.WorldToScreenPoint(chosenPlane.transform.position);
for (j = 0;j<4;j++) {
var upgradeContent : GUIContent = GUIContent(upgradeTextures[j], j.ToString());
if (GUI.Button(new Rect (pos.x-64,pos.y,64,64), upgradeContent)){
upOption = j;
if (upOption == 0) {Up();}
if (upOption == 1) {Sell();}
if (upOption == 3) {Cancel();}
if (upOption == 2) {Repair();}
}
else if(pos.x <= Screen.width) {pos.x+=64;}
else {pos.y+=64;}
}
Robertbu, can you show me the idea make an extra translation, Im not good at these function at all.
So to convert to GUI, you would add this between lines 1 and 2:
pos.y = Screen.height - pos.y;
I'm not sure that is your problem given the positioning on the screen.
Oh man, it works perfectly xD Can you explain why I must convert the variable "pos.y" like that?
Your answer
![](https://koobas.hobune.stream/wayback/20220613111627im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate cloned prefab to local position of an empty object 1 Answer
Teleport script, who can help me? 2 Answers
Will this code move an object 2 Answers
Disable/enable script and animation when you move your mouse cursor 1 Answer
Help! How to change Real numbers to Natural numbers on Distance Counter script (Noob question) 0 Answers