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 Mossey · Dec 02, 2013 at 11:41 PM · worldtoscreenpoint

Onscreen objective pointer

I would like to create a gui texture which "points"(i.e. is placed at) a physical 3d object. I read alot about this line of code:

 Camera.mainCamera.WorldToScreenPoint(target.transform.position);

But when I implement it, it gives me strange values. At some stages it gives me something like 7500 for my x. The tiny game screen in the centre only displays a range of 0->0.9 for x and y, so any value above 1 is not renderable.

How can I implement WorldToScreenPoint correctly?

 #pragma strict
 
 public var objectiveIcon : GUITexture;
 public var objectiveDescription : GUIText;
 private var icon : GUITexture;
 private var description : GUIText;
 
 function Start ()
 {
     icon = Instantiate(objectiveIcon);
     description = Instantiate(objectiveDescription);
 }
 
 function Update ()
 {
     icon.transform.position = Camera.mainCamera.WorldToScreenPoint (this.transform.position);
     icon.transform.position.y = Screen.height - icon.transform.position.y;
     description.transform.position = Camera.mainCamera.WorldToScreenPoint (this.transform.position);
     description.transform.position.y = Screen.height - icon.transform.position.y;
     Debug.Log(icon.transform.position.ToString());
     Debug.Log(description.transform.position.ToString());
 }
 
 function OnDestroy ()
 {
     Destroy(icon);
     Destroy(description);
 }
Comment
Add comment
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

1 Reply

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

Answer by robertbu · Dec 02, 2013 at 11:45 PM

WorldToScreenPoint() returns the positions in screen pixels. But a GUITexture lives in Viewport space. Viewport space starts at 0,0 in the bottom left corner and goes to 1,1 in the upper right. So to place a GUITexture you want to use Camera.WorldToViewportPoint().

 transform.position = Camera.main.WorldToViewportPoint(target.transform.position);

Also with this logic, you don't want to be adjusting the 'y' height of the position using screen coordinates as you do on line 19. You can adjust where to anchor the image using the PixelInset rect associated with the GUITexture. In the particular case of your logic above, you will want to set your anchor point to the middle top of the texture and then you will set your 'y' position to 1.0.

Comment
Add comment · Show 5 · 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 Mossey · Dec 02, 2013 at 11:52 PM 0
Share

Thank you very much!

Uh, sorry about that Y thing. I did it only because everybody wrote that onscreen Y works inversely with WorldToScrenPoint.

avatar image robertbu · Dec 03, 2013 at 12:01 AM 0
Share

There is a difference between a GUITexture and GUI.DrawTexture. The latter is from the GUI interface and lives in GUI coordinates. GUI coordiantes are screen coordinates that start in the upper left corner rather than the lower left corner. So if you were trying to place a GUI.DrawTexture at a world position you would first convert to screen coordinates and then you would have to do make the conversion that 'everybody wrote' about.

avatar image Mossey · Dec 03, 2013 at 12:14 AM 0
Share

Okay everything works now, but I get a strange glitch. $$anonymous$$y objective pointer is created once, but appears in two places in the scene. One is at the objective, the other one is somewhere far away outside the map. The code didn't change much:

 function Update ()
 {
     icon.transform.position = Camera.mainCamera.WorldToViewportPoint(this.transform.position);
     description.transform.position = Camera.mainCamera.WorldToViewportPoint(this.transform.position);
     
     icon.pixelInset.y = 20;
     description.pixelOffset.x = -45;
     description.pixelOffset.y = 20;
 }

objectiveIcon and objectiveDescription are prefabs, where icon and description are instances. They both get destroyed when I reach the objective.

avatar image robertbu · Dec 03, 2013 at 12:22 AM 0
Share

Any chance you have two camera viewing the scene?

avatar image Mossey · Dec 03, 2013 at 12:24 AM 0
Share

I have a top-view $$anonymous$$imap camera. Its settings exclude the layer that the scripted object is in. Any way I can debug / fix?

@Edit the second camera doesn't have a guiLayer either. When I disable the guiLayer on $$anonymous$$ain Camera both guiTextures disappear.

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

Doing a Crosshair for a Space Fight Simulator 0 Answers

Lock On Reticle 1 Answer

How to get size in pixels of an object using SteamVR camera 0 Answers

Worldtoscreenpoint..... 2 Answers

WorldToScreenPoint, isn't getting it right? 1 Answer


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