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 dzuncoi · Aug 06, 2013 at 08:04 AM · position

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.

Comment
Add comment · Show 4
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 Joyrider · Aug 06, 2013 at 08:53 AM 0
Share

What you want is the object's screen position?

avatar image dzuncoi · Aug 06, 2013 at 02:47 PM 0
Share

Yes, that's it. I know to use worldToScreenPoint but I still get some trouble that I post in Paulius's answer below :)

avatar image robertbu · Aug 06, 2013 at 03:10 PM 0
Share

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.

avatar image dzuncoi · Aug 06, 2013 at 03:40 PM 0
Share

I use GUI man :( Can we discuss in the answer below, I already posted my code there.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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).

Comment
Add comment · Show 4 · 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 dzuncoi · Aug 06, 2013 at 02:53 PM 0
Share

I know to use it but I still have some troubles. Here is it: alt text

alt text

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.

1.jpg (369.0 kB)
2.jpg (372.7 kB)
avatar image dzuncoi · Aug 06, 2013 at 03:18 PM 0
Share

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.

avatar image robertbu · Aug 06, 2013 at 05:10 PM 0
Share

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.

avatar image dzuncoi · Aug 07, 2013 at 12:07 PM 0
Share

Oh man, it works perfectly xD Can you explain why I must convert the variable "pos.y" like that?

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

16 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

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


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